我有一个窗口窗体。当我单击 Windows 窗体控制框的关闭 (X) 按钮时,我想显示一条消息或可能执行某些操作。
下面是代码:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$frmTest = New-Object System.Windows.Forms.Form
$frmTest.Size = New-Object System.Drawing.Size(640,480)
$frmTest.MaximizeBox = $False
$frmTest.ShowDialog()
当用户单击“关闭”(X) 按钮时,我想显示一个消息框:
$choice = [System.Windows.Forms.MessageBox]::Show('Are you you want to exit?','TEST','YesNo','Error')
switch($choice)
{
'Yes'
{
$frmTest.Close()
}
}
我找到了这篇文章:表单上的消息关闭 https://www.sapien.com/forums/viewtopic.php?t=10518,但我不知道如何使用它。请指教。谢谢
要捕获的事件是Closing https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.closing?view=netframework-4.8具有事件参数的形式的事件,允许您取消事件。要了解如何在 PowerShell 中使用事件参数,您可能需要查看Windows 窗体控制 PowerShell 中的事件 - 使用 Sender 和 EventArgs https://stackoverflow.com/a/51130615/3110834.
Example
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text ="Test"
$form.Add_Closing({param($sender,$e)
$result = [System.Windows.Forms.MessageBox]::Show(`
"Are you sure you want to exit?", `
"Close", [System.Windows.Forms.MessageBoxButtons]::YesNoCancel)
if ($result -ne [System.Windows.Forms.DialogResult]::Yes)
{
$e.Cancel= $true
}
})
$form.ShowDialog() | Out-Null
$form.Dispose()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)