禁用Alt + F4,但允许使用代码CloseReason关闭表单. [英] Disable Alt+F4 but allow the form to be closed by code, CloseReason.UserClosing is not helping
本文介绍了禁用Alt + F4,但允许使用代码CloseReason关闭表单.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望通过执行 Alt + F4 不会关闭该表单,但是如果从同一表单调用Application.Exit()
或this.Close
,则应将其关闭
I want that the form will not close by doing Alt + F4 but if Application.Exit()
or this.Close
is called from the same Form, it should be closed.
我尝试了CloseReason.UserClosing
,但仍然没有帮助.
I tried CloseReason.UserClosing
but still no help.
推荐答案
如果仅需要过滤 Alt + F4 事件(请单击关闭框,this.Close()
和Application.Exit()
照常运行),那么我可以提出以下建议:
If you need to filter out Alt + F4 event only (leaving clicking of close box, this.Close()
and Application.Exit()
to behave as usual) then I can suggest the following:
- 设置表单的
KeyPreview
属性true
; -
连接表单的
KeyDown
事件:
- Set form's
KeyPreview
property totrue
; Wire up form's
FormClosing
andKeyDown
events:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_altF4Pressed)
{
if (e.CloseReason == CloseReason.UserClosing)
e.Cancel = true;
_altF4Pressed = false;
}
}
private bool _altF4Pressed;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F4)
_altF4Pressed = true;
}
这篇关于禁用Alt + F4,但允许使用代码CloseReason关闭表单.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文