Board logo

标题: [转载教程] 玩转PowerShell之(7)图形菜单 [打印本页]

作者: HAT    时间: 2013-10-29 14:47     标题: 玩转PowerShell之(7)图形菜单

玩转PowerShell之(1)检测硬盘自由空间http://bbs.bathome.net/thread-27008-1-1.html
玩转PowerShell之(2)计算篇http://bbs.bathome.net/thread-27010-1-1.html
玩转PowerShell之(3)命令执行结果http://bbs.bathome.net/thread-27011-1-1.html
玩转PowerShell之(4)email篇http://bbs.bathome.net/thread-27012-1-1.html
玩转PowerShell之(5)偷偷下载http://bbs.bathome.net/thread-27013-1-1.html
玩转PowerShell之(6)数据库篇http://bbs.bathome.net/thread-27014-1-1.html
玩转PowerShell之(7)图形菜单http://bbs.bathome.net/thread-27016-1-1.html
玩转PowerShell之(8)email篇续  - 把gmail当相册http://bbs.bathome.net/thread-27017-1-1.html
玩转PowerShell之(9)DNShttp://bbs.bathome.net/thread-27018-1-1.html

在PS之前,如果我希望为用户提供一个复杂点儿的图形菜单,如果我用vbs的话,一般会使用hta来实现。而PowerShell下面则可以利用.net来实现windows gui风格的菜单。
下面,我来和大家一起实现一个不算很复杂的菜单。菜单里有两个输入框,让用户来输入名字和姓。然后还有一个多选框来让用户选择最喜欢的数字。最后当用户点ok按钮之后,会在PS console下面显示 "hi 谁谁谁, 你最喜欢的数字是 xxx"

下面就是菜单的代码,我已经做了中文的注解,理解起来应该不是很难。

  1. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  2. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  3. #生成菜单框,大小为300x400,在屏幕中央显示
  4. $objForm = New-Object System.Windows.Forms.Form
  5. $objForm.Text = "PowerShell Menu"
  6. $objForm.Size = New-Object System.Drawing.Size(300,400)
  7. $objForm.StartPosition = "CenterScreen"
  8. #下面生成第一个输入框,包括输入提示文本,和输入框。提示用户输入名字,这里设定了一个缺省名为Greg
  9. $objLabel = New-Object System.Windows.Forms.Label
  10. $objLabel.Location = New-Object System.Drawing.Size(10,20)
  11. $objLabel.Size = New-Object System.Drawing.Size(280,20)
  12. $objLabel.Text = "Please enter your First name:"
  13. $objForm.Controls.Add($objLabel)
  14. $objTextBox = New-Object System.Windows.Forms.TextBox
  15. $objTextBox.Location = New-Object System.Drawing.Size(10,40)
  16. $objTextBox.Size = New-Object System.Drawing.Size(260,20)
  17. $objTextBox.text = "Greg"
  18. $objForm.Controls.Add($objTextBox)
  19. #下面生成第二个输入框,包括输入提示文本,和输入框。提示用户输入姓,这里设定了一个缺省值为Wang
  20. $objLabel1 = New-Object System.Windows.Forms.Label
  21. $objLabel1.Location = New-Object System.Drawing.Size(10,60)
  22. $objLabel1.Size = New-Object System.Drawing.Size(280,20)
  23. $objLabel1.Text = "Please enter your surname:"
  24. $objForm.Controls.Add($objLabel1)
  25. $objTextBox1 = New-Object System.Windows.Forms.TextBox
  26. $objTextBox1.Location = New-Object System.Drawing.Size(10,80)
  27. $objTextBox1.Size = New-Object System.Drawing.Size(260,20)
  28. $objTextBox1.text = "Wang"
  29. $objForm.Controls.Add($objTextBox1)
  30. #生成一多选框,要求用户选择最喜欢的数字
  31. $objLabel2 = New-Object System.Windows.Forms.Label
  32. $objLabel2.Location = New-Object System.Drawing.Size(10,100)
  33. $objLabel2.Size = New-Object System.Drawing.Size(280,20)
  34. $objLabel2.Text = "Please enter your favorite number:"
  35. $objForm.Controls.Add($objLabel2)
  36. $number=0..9
  37. $objListbox = New-Object Windows.Forms.CheckedListBox
  38. $objListbox.CheckOnClick = $true
  39. $objListBox.Items.AddRange($number)
  40. $objListBox.Location=New-Object System.Drawing.Size(10,120)
  41. $objListBox.Size = New-Object System.Drawing.Size(260,160)
  42. $objForm.Controls.Add($objListBox)
  43. #生成OK按钮
  44. $OKButton = New-Object System.Windows.Forms.Button
  45. $OKButton.Location = New-Object System.Drawing.Size(110,320)
  46. $OKButton.Size = New-Object System.Drawing.Size(75,23)
  47. $OKButton.Text = "OK"
  48. $OKButton.Add_Click({$objForm.Close()})
  49. $objForm.Controls.Add($OKButton)
  50. #生成结果
  51. $objForm.Add_Shown({$objForm.Activate()})
  52. [void] $objForm.ShowDialog()
  53. "Hi "+$objTextBox.text+" "+$objTextBox1.text+"! Your favorite number is:"
  54. foreach($index in $objListBox.CheckedIndices)
  55. {
  56. $number[$index]
  57. }
复制代码


http://msdn2.microsoft.com/en-us/library/system.windows.forms.aspx

http://bbs.winos.cn/thread-28891-1-1.html




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2