- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
-
- $Form1 = New-Object System.Windows.Forms.Form
- $Form1.ClientSize = New-Object System.Drawing.Size(407, 390)
- $form1.topmost = $true
-
- #组合框1
- $computerNames = @("中国", "日本", "美国")
- $comboBox1 = New-Object System.Windows.Forms.ComboBox
- $comboBox1.Location = New-Object System.Drawing.Point(25, 55)
- $comboBox1.Size = New-Object System.Drawing.Size(350, 310)
- foreach ($computer in $computerNames) {
- $comboBox1.Items.add($computer)
- }
- $Form1.Controls.Add($comboBox1)
-
- #组合框2
- $computerNames2 = @("北京", "上海", "广州")
- $comboBox2 = New-Object System.Windows.Forms.ComboBox
- $comboBox2.Location = New-Object System.Drawing.Point(25, 120)
- $comboBox2.Size = New-Object System.Drawing.Size(350, 310)
- foreach ($computer in $computerNames2) {
- $comboBox2.Items.add($computer)
- }
- $Form1.Controls.Add($comboBox2)
-
- #按钮
- $Button = New-Object System.Windows.Forms.Button
- $Button.Location = New-Object System.Drawing.Point(25, 20)
- $Button.Size = New-Object System.Drawing.Size(98, 23)
- $Button.Text = "输出所选"
- $Button.add_Click( { $label.Text = $comboBox1.SelectedItem.ToString() })
- $Form1.Controls.Add($Button)
-
- #标签
- $label = New-Object System.Windows.Forms.Label
- $label.Location = New-Object System.Drawing.Point(70, 90)
- $label.Size = New-Object System.Drawing.Size(98, 23)
- $label.Text = ""
- $Form1.Controls.Add($label)
-
- #热键
- $Form1.KeyPreview = $true
- $Form1.Add_KeyDown( {
- param($oSender, $oArgs)
- # 创建一个小数组来捕获使用的修饰键
- $modifiers = @()
- # Write-Host $oArgs.KeyCode
- if ($oArgs.Shift) { $modifiers += "Shift" }
- if ($oArgs.Alt) { $modifiers += "Alt" }
- if ($oArgs.Control) { $modifiers += "Control" }
- # 使用该数组,构建部分输出文本
- $modkeys = ''
- if ($modifiers.Count) {
- $modkeys = '{0} ' -f ($modifiers -join ' + ')
- }
-
- # 而不是建立一个字符串 'Shift + Control + Alt', 像上面,你也可以使用
- # $oArgs.Modifiers 属性,并将逗号替换为加号,如下所示:
- # $modkeys = $oArgs.Modifiers -replace ', ', ' + '
-
- #这些是您想要作为示例做出反应的关键
- #在这里,按下的所有键都只是做同样的事情,即显示按下的内容,
- #因此,我们可以缩短代码,以简单地测试
- #数组与用户按下的键相对应。
-
- if ('Q', 'A', 'F1', 'Escape', 'NumLock' -contains $oArgs.KeyCode) {
-
- # 我们通过在 POwershell 中使用 '-f' Format 运算符来创建输出字符串。
- Write-Host ('{0}{1} pressed' -f $modkeys, $oArgs.KeyCode)
-
- #同样的事情也可以用这样的东西来完成:
- # Write-Host ($modkeys + ' ' + $oArgs.KeyCode + ' pressed')
- #或者
- # Write-Host "$modkeys $($oArgs.KeyCode) pressed"
- }
- })
-
- $form1.showdialog()
- $Form1.Dispose()
复制代码
|