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.
I tried CloseReason.UserClosing
但仍然没有帮助。
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 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx财产给
true
;
-
连接表格FormClosing http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx and KeyDown http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)