Monday 13 May 2013

Powershell Drawing, Form, Buttons, Dialogs

The System.Windows.Forms and System.Drawing are great classes which give us some nice ways to present our applications to users.

Before you can use them you must load them. So copy the 2 lines below to your script or shell.

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null


 

Now we can start creating our own GUI.
First let's first create a form:

$Form = New-Object System.Windows.Forms.Form
$Form.width = 270
$Form.height = 55
$Form.Text = "Form Title"
$Form.backcolor = "black"
$Form.maximumsize = New-Object System.Drawing.Size(270,55)
$Form.minimumsize = New-Object System.Drawing.Size(270,55)
$Form.startposition = "centerscreen"


 

This will create a form with black background, a fixed size of 270x55 and it will be displayed in the center of the screen.
Note that the width is measure from the left to the right and the height from north to south. So the point 0 of the form would be its left upper corner.

Here are some extras you can add to your form.
This gives a 3D style border style:

$Form.FormBordStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D

Other values are: FixedSingle, FixedDialog, FixedToolWindow, None, Sizable, SizableToolWindow
This would remove the controlbox where you can find the Close, Minimize and Maximize buttons.

$Form.ControlBox = $false

This will return a custom action when the Enter key is pressed or will close the form when the Esc key is pressed:

$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {Your Custom Action here}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$Form.Close()}})


Now let's add some interaction to our form starting with a dropdown menu:

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,0)
$DropDown.Size = new-object System.Drawing.Size(160,30)


Of course you will have to add items to the dropdown menu and that's how you do it:

$DropDown.Items.Add($Item) | Out-Null

Time to create some buttons.

$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(60,0)
$OKButton.Size = new-object System.Drawing.Size(40,25)
$OKButton.BackColor = "Transparent"
$OKButton.Text = "OK"
$OKButton.ForeColor = "White"
$OKButton.Add_Click({Your Custom Action here})


This will create an OK button which will be black as the form is black and the button is transparent with white text. You can add a custom action to the Add_Click method.
Once you are finished with the form and all other objects then it's required to put everything together:

$Form.Controls.Add($DropDown)
$Form.Controls.Add($OKButton)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()


And that's how you can open a dialog to allow the user to select a file:

$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = "C:\"
$objForm.Filter = "All Files (*.*)|*.*"
$objForm.Title = "Source"
$objForm.ShowHelp = $true
$show = $objForm.ShowDialog()


These are the basics. A lot more can be done. Explore all the methods and properties of an object by creating it and piping it to the Get-Member cmdlet.

For example:

$OKButton = new-object System.Windows.Forms.Button
$OKButton | Get-Member


No comments:

Post a Comment