上一节学习了怎么创建一个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()
|