Advertisement
SX86

PowerShell Windows Forms Tutorial 1 : Hello World

Jul 7th, 2017
6,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Load required assemblies
  2. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  3.  
  4. # Drawing form and controls
  5. $Form_HelloWorld = New-Object System.Windows.Forms.Form
  6.     $Form_HelloWorld.Text = "Hello World"
  7.     $Form_HelloWorld.Size = New-Object System.Drawing.Size(272,160)
  8.     $Form_HelloWorld.FormBorderStyle = "FixedDialog"
  9.     $Form_HelloWorld.TopMost = $true
  10.     $Form_HelloWorld.MaximizeBox = $false
  11.     $Form_HelloWorld.MinimizeBox = $false
  12.     $Form_HelloWorld.ControlBox = $true
  13.     $Form_HelloWorld.StartPosition = "CenterScreen"
  14.     $Form_HelloWorld.Font = "Segoe UI"
  15.  
  16. # adding a label to my form
  17. $label_HelloWorld = New-Object System.Windows.Forms.Label
  18.     $label_HelloWorld.Location = New-Object System.Drawing.Size(8,8)
  19.     $label_HelloWorld.Size = New-Object System.Drawing.Size(240,32)
  20.     $label_HelloWorld.TextAlign = "MiddleCenter"
  21.     $label_HelloWorld.Text = "Hello World"
  22.     $Form_HelloWorld.Controls.Add($label_HelloWorld)
  23.  
  24. # add a button
  25. $button_ClickMe = New-Object System.Windows.Forms.Button
  26.     $button_ClickMe.Location = New-Object System.Drawing.Size(8,80)
  27.     $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
  28.     $button_ClickMe.TextAlign = "MiddleCenter"
  29.     $button_ClickMe.Text = "Click Me!"
  30.     $button_ClickMe.Add_Click({
  31.         $button_ClickMe.Text = "You did click me!"
  32.         Start-Process calc.exe
  33.     })
  34.     $Form_HelloWorld.Controls.Add($button_ClickMe)
  35.  
  36.  
  37. # show form
  38. $Form_HelloWorld.Add_Shown({$Form_HelloWorld.Activate()})
  39. [void] $Form_HelloWorld.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement