禁用ALT + F4但允许的形式,由code被关闭,CloseReason.UserClosing是没有帮助 [英] Disable Alt+F4 but allow the the form to be closed by code, CloseReason.UserClosing is not helping

查看:440
本文介绍了禁用ALT + F4但允许的形式,由code被关闭,CloseReason.UserClosing是没有帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,窗体不会关闭通过执行<大骨节病>替代 + <大骨节病> 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:

  1. 设置窗体的 键preVIEW 属性;
  2. 电线表单的 的FormClosing 和的 的KeyDown 事件:

  1. Set form's KeyPreview property to true;
  2. Wire up form's FormClosing and KeyDown 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆