如何使用 alt + F4 防止任何表单关闭 [英] How do prevent any Form from closing using alt + F4
问题描述
这不是
并将Aight"按钮单击侦听器设置为:
private void Aight_buton_Click(object sender, EventArgs e){dr = DialogResult.OK;关闭();}
X"按钮的情况也是如此.按照上述问题的答案,我可以这样做:
private void Form1_FormClosing(object sender, FormClosingEventArgs e){e.Cancel = e.CloseReason == CloseReason.UserClosing;}
但由于我在 Aight_buton_Click
下使用 Close()
,它仍然注册为 e.CloseReason == CloseReason.UserClosing;
.所以按下键不会关闭我的表单(自定义消息框),Alt+F4 也不会.我想知道我如何具体地只防止 Alt+F4 关闭而不是 Close()
关闭.请我宁愿不使用 ModifierKeys
因为它不是处理这种情况的最合适也不是最聪明的方法.
自己处理 Atl+F4
并设置它处理.
在表单构造函数中先设置
this.KeyPreview = true;
然后处理keyDown事件
private void Form1_KeyDown(object sender, KeyEventArgs e){if (e.Alt && e.KeyCode == Keys.F4){e.handled = true;}}
This is not a duplicate of How to Disable Alt + F4 closing form?. Please read why.
I have made a custom MessageBox under my main Form.
And have set "Aight" button click listener as:
private void Aight_buton_Click(object sender, EventArgs e)
{
dr = DialogResult.OK;
Close();
}
The same is the case with the "X" button. Following the above question's answer I could have done this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = e.CloseReason == CloseReason.UserClosing;
}
but since I am using Close()
under the Aight_buton_Click
it still registers as e.CloseReason == CloseReason.UserClosing;
. So hitting the key doesn't close my form (a custom messagebox) nor does Alt+F4. I would like to know how specifically I can prevent only Alt+F4 closes and not the Close()
closes. And please I would rather not use ModifierKeys
as it is not the most appropriate not the smartest way to handle this situation.
Handle Atl+F4
by your self and set it handled.
In the form constructor first set
this.KeyPreview = true;
Then handle the keyDown event
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F4)
{
e.Handled = true;
}
}
这篇关于如何使用 alt + F4 防止任何表单关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!