以Powershell形式禁用Alt + F4 [英] Disabling Alt+F4 in a Powershell form
本文介绍了以Powershell形式禁用Alt + F4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个Powershell脚本,该脚本使用 System.Windows.Forms
显示一个表单.我已经禁用了控制框以及通过鼠标关闭此表单的所有其他方式.但是我找不到通过按 Alt + F4 防止表单关闭的方法.
I've written a powershell script which displays a form using System.Windows.Forms
. I've already disabled the control box and all other ways that this form can be closed via the mouse. But I can't find a way of preventing the form closing by pressing Alt+F4.
即代码段如下所示:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Restart Required"
$objForm.Size = New-Object System.Drawing.Size(400,300)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$objForm.MinimizeBox = $false
$objForm.MaximizeBox = $false
$objForm.FormBorderStyle = "Fixed3d"
$objForm.ControlBox = $false
$objForm.ShowInTaskbar = $false
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
Looking at MSDN, there are articles about overriding the FormClosing
eventhandler in VB, C#, etc and . But I'm not sure how to implement similar logic into Powershell (if it's at all possible).
推荐答案
将表单keypreview设置为true
Set forms keypreview to true
$form1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if ($_.Alt -eq $true -and $_.KeyCode -eq 'F4') {
$script:altF4Pressed = $true;
}
}
$form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
if ($script:altF4Pressed)
{
if ($_.CloseReason -eq 'UserClosing') {
$_.Cancel = $true
$script:altF4Pressed = $false;
}
}
}
这篇关于以Powershell形式禁用Alt + F4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文