本帖最后由 小白龙 于 2023-4-13 13:22 编辑
我想使用Ctrl+回车热键关闭form窗口, 并返回编辑框内的文本, 下面的代码没有效果, 求路过大佬指导- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
-
- # 创建表单和控件
- $form = New-Object System.Windows.Forms.Form
- $button = New-Object System.Windows.Forms.Button
- $textbox = New-Object System.Windows.Forms.TextBox
-
- # 配置表单属性
- $form.Text = "My Form"
- $form.Size = New-Object System.Drawing.Size(300, 200)
- $form.StartPosition = "CenterScreen"
-
- # 配置按钮属性
- $button.Text = "Print Text"
- $button.Location = New-Object System.Drawing.Point(100, 80)
- $button.add_Click({
- Write-Host $textbox.Text
- })
-
- # 配置文本框属性
- $textbox.Location = New-Object System.Drawing.Point(50, 40)
- $textbox.Size = New-Object System.Drawing.Size(200, 20)
-
- # 将控件添加到表单上
- $form.Controls.Add($button)
- $form.Controls.Add($textbox)
-
- # 注册Ctrl+Enter热键并处理窗口关闭事件
- $Form.KeyPreview = $true
- $form.Add_KeyDown({
- if ($_.KeyCode -eq "Enter" -and $_.Control)
- {
- $form.Close()
- $textbox.Text
- }
- })
-
- # 显示表单
- [void]$form.ShowDialog()
复制代码
|