Let’s say you want to inform your users about something, but you need to know what they pressed, you need to have custom buttons, with tooltips, messages, sounds and so on.
Fear not, you can achieve all of this with Powershell.
As you can see, we have a custom messagebox with many features.
First of all, 90% of the form is represented by an image. The form is then dynamically resized after the image size. You can inform the user about the required activities by just editing an image and just paste the location in your code.
On the top you can add your own logo and text.
If you ever forget to add something in your image, or want something more dynamical, you can add a label on the bottom of the image.
If the user hovers over the buttons, you can add a hover action in form of a ToolTip, informing him what happens when he clicks it.
The buttons can be resized, placed and customized as you wish.
To make sure the user doesn’t select by mistake any button, you can add an additional confirmation dialog. Of course you can catch the user input from that dialog and see how to further proceed.
You even have a nice little sound notification present in Windows when the form shows up.
PS: Before you test the code, make sure your image.jpg is 550x550px to get the same results as in the screenshots.
The code:
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms") $nl = [System.Environment]::NewLine $Global:ret=$false $ScriptPath = $MyInvocation.MyCommand.Path $Global:ScriptDir = split-path -parent $ScriptPath function ExitWithCode { param ( $exitcode ) $host.SetShouldExit($exitcode) [Environment]::Exit($exitcode) } $location = $Global:ScriptDir + "\image.jpg" $file = (get-item $location) $img = [System.Drawing.Image]::Fromfile($file); [System.Windows.Forms.Application]::EnableVisualStyles(); $form = new-object Windows.Forms.Form $form.Text = "Powershell Form Example" $form.Width = $img.Size.Width + 15; $form.Height = $img.Size.Height + 125; $Icon = New-Object system.drawing.icon ($Global:ScriptDir + "\logo.ico") $form.Icon = $Icon $pictureBox = new-object Windows.Forms.PictureBox $pictureBox.Width = $img.Size.Width; $pictureBox.Height = $img.Size.Height; $pictureBox.Image = $img; $Label = New-Object System.Windows.Forms.Label $Font = New-Object System.Drawing.Font("Times New Roman",10,[System.Drawing.FontStyle]::Bold) $Label.Text = "Note: You can add how many labels you want in the form" $Label.AutoSize = $True $Label.Location = New-Object System.Drawing.Size(120, 550 ) $Label.Font = $Font $Form.Controls.Add($Label) $Button1DrawingLocationX = "15" $Button1DrawingLocationY = "570" $Button1DrawingSizeX = "250" $Button1DrawingSizeY = "45" $Button2DrawingLocationX = "275" $Button2DrawingLocationY = "570" $Button2DrawingSizeX = "250" $Button2DrawingSizeY = "45" $SelectButton = New-Object System.Windows.Forms.Button $SelectButton.Location = New-Object System.Drawing.Size($Button1DrawingLocationX, $Button1DrawingLocationY ) $SelectButton.Size = New-Object System.Drawing.Size($Button1DrawingSizeX,$Button1DrawingSizeY) $SelectButton.Text = "Yes" $SelectButton.Name = "YES" $tooltip1 = New-Object System.Windows.Forms.ToolTip $tipMigrate = "Click here to select YES" $tooltip1.SetToolTip($SelectButton, $tipMigrate) $SelectButton.Add_MouseEnter({$tooltip1}) $SelectButton2 = New-Object System.Windows.Forms.Button $SelectButton2.Location = New-Object System.Drawing.Size($Button2DrawingLocationX,$Button2DrawingLocationY) $SelectButton2.Size = New-Object System.Drawing.Size($Button2DrawingSizeX,$Button2DrawingSizeY) $SelectButton2.Text = "NO" $SelectButton2.Name = "NO" $tooltip2 = New-Object System.Windows.Forms.ToolTip $tipPostpone = "Click here to select NO" $tooltip2.SetToolTip($SelectButton2, $tipPostpone) $SelectButton2.Add_MouseEnter({$tooltip2}) $SelectButton2.TabIndex = 1 $SelectButton.TabIndex = 2 Function DisplayMB { Add-Type -AssemblyName PresentationFramework $msgBoxInput = [System.Windows.MessageBox]::Show('Are you sure you want to press this button?','Confirm','YesNo','Question') switch ($msgBoxInput) { 'Yes' { $Global:ret=$true $form.Close() } 'No' { } } } Function DisplayMBCancel { Add-Type -AssemblyName PresentationFramework $msgBoxInput = [System.Windows.MessageBox]::Show('You never press NO!','Notice','Ok','Warning') switch ($msgBoxInput) { 'Ok' { $Global:ret=$false $form.Close() } } } $SelectButton.Add_Click( { DisplayMB } ) $SelectButton2.Add_Click( { DisplayMBCancel } ) $form.Controls.Add($SelectButton) $form.Controls.Add($SelectButton2) $form.controls.add($pictureBox) $SoundFile = "$env:SystemDrive\Windows\Media\Windows Notify.wav" $SoundPlayer = New-Object System.Media.SoundPlayer -ArgumentList $SoundFile $SoundPlayer.Add_LoadCompleted({ $This.Play() $This.Dispose() }) $SoundPlayer.LoadAsync() $form.Add_Shown( { $form.Activate() } ) $a= $form.ShowDialog() If ($Global:ret){ ExitWithCode -exitcode 0} Else{ ExitWithCode -exitcode 1602}