建立自定義輸入方塊

此範例僅適用於 Windows 平臺。

在 Windows PowerShell 3.0 和更新版本中,使用 Microsoft .NET Framework 窗體建置功能編寫圖形化自定義輸入方塊的腳本。

建立自訂圖形化輸入方塊

複製下列內容,然後將下列內容貼到 Windows PowerShell ISE 中,然後將它儲存為 PowerShell 腳本 (.ps1) 檔案。

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}

腳本一開始會載入兩個 .NET Framework 類別: System.DrawingSystem.Windows.Forms。 然後,您會啟動 .NET Framework 類別 System.Windows.Forms.Form 的新實例。 這會提供您可以開始新增控制件的空白表單或視窗。

$form = New-Object System.Windows.Forms.Form

建立 Form 類別的實體之後,請將值指派給這個類別的三個屬性。

  • Text (文字)。 這會成為視窗的標題。
  • 大小。 這是表單的大小,以像素為單位。 上述腳本會建立寬度為 300 像素的表單,高度為 200 像素。
  • StartingPosition。 在上述腳本中,這個選擇性屬性會設定為 CenterScreen 。 如果您未新增此屬性,Windows 會在開啟表單時選取位置。 藉由將 StartingPosition 設定為 CenterScreen,您就會在每次載入時自動在畫面中間顯示表單。
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

接下來,建立表單的 [確定] 按鈕。 指定 [確定] 按鈕的大小和行為。 在此範例中,按鈕位置距離窗體的上邊緣為120圖元,而左邊緣則為75圖元。 按鈕高度為 23 像素,而按鈕長度為 75 像素。 腳本會使用預先定義的 Windows Forms 類型來判斷按鈕行為。

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

同樣地,您會建立 [取消] 按鈕。 [ 取消 ] 按鈕距離頂端為 120 像素,但距離視窗左邊緣 150 像素。

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

接下來,在您的視窗上提供標籤文字,描述您希望使用者提供的資訊。

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

新增控制項(在此案例中為文字),讓使用者提供標籤文字中所描述的資訊。 除了文本框之外,您還可以套用許多其他控制件。 如需更多控件,請參閱 System.Windows.Forms 命名空間

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

Topmost 屬性設定為 $true ,強制窗口開啟其他開啟視窗和對話方塊。

$form.Topmost = $true

接下來,新增這一行程式代碼以啟動窗體,並將焦點設定為您建立的文字框。

$form.Add_Shown({$textBox.Select()})

新增下列程式代碼行,以在 Windows 中顯示表單。

$result = $form.ShowDialog()

最後,If 區塊內的程式代碼會指示 Windows 在使用者提供文字之後,對表單執行什麼動作,然後按兩下 [確定] 按鈕或按 Enter 鍵。

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    $x = $textBox.Text
    $x
}

另請參閱