禁用ALT + F4但允许的形式,由code被关闭,CloseReason.UserClosing是没有帮助 [英] Disable Alt+F4 but allow the the form to be closed by code, CloseReason.UserClosing is not helping
问题描述
我想,窗体不会关闭通过执行<大骨节病>替代骨节病> + <大骨节病> 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.
推荐答案
如果您需要过滤掉<大骨节病>替代骨节病> + <大骨节病> 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:
- 设置窗体的
键preVIEW
属性真
; -
电线表单的
的FormClosing
和的的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但允许的形式,由code被关闭,CloseReason.UserClosing是没有帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!