1. 程式人生 > >PowerShell GUI之建立Button和輸入框

PowerShell GUI之建立Button和輸入框

上一節學習了怎麼建立一個form,更改顏色,字型背景什麼的,這節課來學習如何新增button和輸入框。

效果圖如下:


程式碼原文:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$PowerShellForms = New-Object System.Windows.Forms.Form 
$PowerShellForms.Text = "The Second form"
$PowerShellForms.Size = New-Object System.Drawing.Size(300,300) 
$PowerShellForms.StartPosition = "CenterScreen"
$PowerShellForms.SizeGripStyle = "Hide"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,160)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "確定"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$PowerShellForms.AcceptButton = $OKButton
$PowerShellForms.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,160)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$PowerShellForms.CancelButton = $CancelButton
$PowerShellForms.Controls.Add($CancelButton)

$Labels1 = New-Object System.Windows.Forms.Label
$Labels1.Location = New-Object System.Drawing.Point(10,20) 
$Labels1.Size = New-Object System.Drawing.Size(280,20) 
$Labels1.Text = "請輸入你的名字:"
$Labels1.AutoSize = $True
$PowerShellForms.Controls.Add($Labels1) 

$Labels2 = New-Object System.Windows.Forms.Label
$Labels2.Location = New-Object System.Drawing.Point(10,90) 
$Labels2.Size = New-Object System.Drawing.Size(280,20) 
$Labels2.Text = "請輸入你的職位:"
$Labels2.AutoSize = $True
$PowerShellForms.Controls.Add($Labels2)

$TextBox1 = New-Object System.Windows.Forms.TextBox 
$TextBox1.Location = New-Object System.Drawing.Point(10,50) 
$TextBox1.Size = New-Object System.Drawing.Size(260,20) 
$PowerShellForms.Controls.Add($TextBox1) 

$TextBox2 = New-Object System.Windows.Forms.TextBox 
$TextBox2.Location = New-Object System.Drawing.Point(10,120) 
$TextBox2.Size = New-Object System.Drawing.Size(260,20) 
$PowerShellForms.Controls.Add($TextBox2)

$PowerShellForms.Topmost = $True

$result = $PowerShellForms.ShowDialog()