创建图形日期选取器Creating a Graphical Date Picker

本文内容

使用 Windows PowerShell 3.0 和更高版本创建一个带有日历式图形控件的窗体,该控件使用户可以选择本月的某一天。

创建图形日期选取器控件Create a graphical date-picker control

复制以下内容并将其粘贴到 Windows PowerShell ISE 中,然后将其另存为 Windows PowerShell 脚本 (.ps1)。

  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3. $form = New-Object Windows.Forms.Form
  4. $form.Text = 'Select a Date'
  5. $form.Size = New-Object Drawing.Size @(243,230)
  6. $form.StartPosition = 'CenterScreen'
  7. $calendar = New-Object System.Windows.Forms.MonthCalendar
  8. $calendar.ShowTodayCircle = $false
  9. $calendar.MaxSelectionCount = 1
  10. $form.Controls.Add($calendar)
  11. $OKButton = New-Object System.Windows.Forms.Button
  12. $OKButton.Location = New-Object System.Drawing.Point(38,165)
  13. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  14. $OKButton.Text = 'OK'
  15. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  16. $form.AcceptButton = $OKButton
  17. $form.Controls.Add($OKButton)
  18. $CancelButton = New-Object System.Windows.Forms.Button
  19. $CancelButton.Location = New-Object System.Drawing.Point(113,165)
  20. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  21. $CancelButton.Text = 'Cancel'
  22. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  23. $form.CancelButton = $CancelButton
  24. $form.Controls.Add($CancelButton)
  25. $form.Topmost = $true
  26. $result = $form.ShowDialog()
  27. if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  28. {
  29. $date = $calendar.SelectionStart
  30. Write-Host "Date selected: $($date.ToShortDateString())"
  31. }

该脚本首先加载两个 .NET Framework 类:System.DrawingSystem.Windows.Forms然后,启动 .NET Framework 类 Windows.Forms.Form 的新实例;它提供一个可以开始添加控件的空白窗体或窗口。

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

在创建 Form 类的实例后,为此类的三个属性赋值。

  • 文本。这将成为该窗口的标题。

  • 大小。这是窗体的大小(以像素为单位)。上述脚本创建的窗体大小为宽 243 像素、高 230 像素。

  • StartingPosition。在上述脚本中,此可选属性将设置为 CenterScreen。如果未添加此属性,Windows 将在窗体打开时选择一个位置。通过将 StartingPosition 设置为 CenterScreen,可使窗体在每次加载时都自动显示在屏幕中间。

  1. $form.Text = 'Select a Date'
  2. $form.Size = New-Object Drawing.Size @(243,230)
  3. $form.StartPosition = 'CenterScreen'

接下来,在窗体中创建并添加一个日历控件。在此示例中,当前日期未突出显示或带圆圈。用户一次只可以在日历上选择一天。

  1. $calendar = New-Object System.Windows.Forms.MonthCalendar
  2. $calendar.ShowTodayCircle = $false
  3. $calendar.MaxSelectionCount = 1
  4. $form.Controls.Add($calendar)

接下来,为窗体创建“确定”按钮。指定“确定”按钮的大小和行为。在此示例中,按钮位置为距窗体上边缘 165 像素,距左边缘 38 像素。按钮高度为 23 像素,按钮长度为 75 像素。此脚本使用预定义的 Windows 窗体类型确定按钮行为。

  1. $OKButton = New-Object System.Windows.Forms.Button
  2. $OKButton.Location = New-Object System.Drawing.Point(38,165)
  3. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  4. $OKButton.Text = 'OK'
  5. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  6. $form.AcceptButton = $OKButton
  7. $form.Controls.Add($OKButton)

采用相同方式创建“取消”按钮。“取消”按钮距窗口上边缘 165 像素,但距左边缘 113 像素。

  1. $CancelButton = New-Object System.Windows.Forms.Button
  2. $CancelButton.Location = New-Object System.Drawing.Point(113,165)
  3. $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  4. $CancelButton.Text = 'Cancel'
  5. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  6. $form.CancelButton = $CancelButton
  7. $form.Controls.Add($CancelButton)

将 Topmost 属性设置为 $True,以强制此窗口在其他已打开的窗口和对话框之上打开。

  1. $form.Topmost = $true

添加以下代码行以在 Windows 中显示该窗体。

  1. $result = $form.ShowDialog()

最后,If 块内的代码指示在用户在日历上选择某一天,然后单击“确定”按钮或按“Enter”键后,Windows 应如何处理该窗体。Windows PowerShell 向用户显示选定的日期。

  1. if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  2. {
  3. $date = $calendar.SelectionStart
  4. Write-Host "Date selected: $($date.ToShortDateString())"
  5. }

另请参阅See Also