当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 创建自定义输入框

作者:精品下载站 日期:2024-12-14 03:01:54 浏览:13 分类:玩电脑

创建自定义输入框


该示例仅适用于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 类的实例后,为该类的三个属性赋值。

  • 文本。这将成为窗口的标题。
  • 大小。这是表单的大小(以像素为单位)。前面的脚本创建了一个宽 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 窗体类型来确定按钮行为。

$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
}

参见

  • GitHub:Dave Wyatt 的 WinFormsExampleUpdates)
  • Windows PowerShell 本周提示:创建自定义输入框

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯