标题: [转载教程] 玩转PowerShell之(7)图形菜单 [打印本页]
作者: HAT 时间: 2013-10-29 14:47 标题: 玩转PowerShell之(7)图形菜单
在PS之前,如果我希望为用户提供一个复杂点儿的图形菜单,如果我用vbs的话,一般会使用hta来实现。而PowerShell下面则可以利用.net来实现windows gui风格的菜单。
下面,我来和大家一起实现一个不算很复杂的菜单。菜单里有两个输入框,让用户来输入名字和姓。然后还有一个多选框来让用户选择最喜欢的数字。最后当用户点ok按钮之后,会在PS console下面显示 "hi 谁谁谁, 你最喜欢的数字是 xxx"
下面就是菜单的代码,我已经做了中文的注解,理解起来应该不是很难。
- [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
- [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
-
- #生成菜单框,大小为300x400,在屏幕中央显示
- $objForm = New-Object System.Windows.Forms.Form
- $objForm.Text = "PowerShell Menu"
- $objForm.Size = New-Object System.Drawing.Size(300,400)
- $objForm.StartPosition = "CenterScreen"
-
- #下面生成第一个输入框,包括输入提示文本,和输入框。提示用户输入名字,这里设定了一个缺省名为Greg
- $objLabel = New-Object System.Windows.Forms.Label
- $objLabel.Location = New-Object System.Drawing.Size(10,20)
- $objLabel.Size = New-Object System.Drawing.Size(280,20)
- $objLabel.Text = "Please enter your First name:"
- $objForm.Controls.Add($objLabel)
-
- $objTextBox = New-Object System.Windows.Forms.TextBox
- $objTextBox.Location = New-Object System.Drawing.Size(10,40)
- $objTextBox.Size = New-Object System.Drawing.Size(260,20)
- $objTextBox.text = "Greg"
- $objForm.Controls.Add($objTextBox)
-
- #下面生成第二个输入框,包括输入提示文本,和输入框。提示用户输入姓,这里设定了一个缺省值为Wang
-
-
- $objLabel1 = New-Object System.Windows.Forms.Label
- $objLabel1.Location = New-Object System.Drawing.Size(10,60)
- $objLabel1.Size = New-Object System.Drawing.Size(280,20)
- $objLabel1.Text = "Please enter your surname:"
- $objForm.Controls.Add($objLabel1)
-
- $objTextBox1 = New-Object System.Windows.Forms.TextBox
- $objTextBox1.Location = New-Object System.Drawing.Size(10,80)
- $objTextBox1.Size = New-Object System.Drawing.Size(260,20)
- $objTextBox1.text = "Wang"
- $objForm.Controls.Add($objTextBox1)
-
- #生成一多选框,要求用户选择最喜欢的数字
- $objLabel2 = New-Object System.Windows.Forms.Label
- $objLabel2.Location = New-Object System.Drawing.Size(10,100)
- $objLabel2.Size = New-Object System.Drawing.Size(280,20)
- $objLabel2.Text = "Please enter your favorite number:"
- $objForm.Controls.Add($objLabel2)
-
- $number=0..9
- $objListbox = New-Object Windows.Forms.CheckedListBox
- $objListbox.CheckOnClick = $true
- $objListBox.Items.AddRange($number)
- $objListBox.Location=New-Object System.Drawing.Size(10,120)
- $objListBox.Size = New-Object System.Drawing.Size(260,160)
- $objForm.Controls.Add($objListBox)
-
- #生成OK按钮
-
- $OKButton = New-Object System.Windows.Forms.Button
- $OKButton.Location = New-Object System.Drawing.Size(110,320)
- $OKButton.Size = New-Object System.Drawing.Size(75,23)
- $OKButton.Text = "OK"
- $OKButton.Add_Click({$objForm.Close()})
- $objForm.Controls.Add($OKButton)
-
- #生成结果
- $objForm.Add_Shown({$objForm.Activate()})
- [void] $objForm.ShowDialog()
-
- "Hi "+$objTextBox.text+" "+$objTextBox1.text+"! Your favorite number is:"
- foreach($index in $objListBox.CheckedIndices)
- {
- $number[$index]
- }
复制代码
http://msdn2.microsoft.com/en-us/library/system.windows.forms.aspx
http://bbs.winos.cn/thread-28891-1-1.html
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |