Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 禁用Alt+;ShowDialog()中的F4_C#_Dialog - Fatal编程技术网

C# 禁用Alt+;ShowDialog()中的F4

C# 禁用Alt+;ShowDialog()中的F4,c#,dialog,C#,Dialog,我读过一些关于禁用Alt+F4的文章,到目前为止,我已经让它部分正常工作了 目前,我在我的主窗体上有这个,它做得很好,只允许在需要时关闭窗体 private void Alerter_FormClosing(object sender, FormClosingEventArgs e) { if (_altF4Pressed) { if (e.CloseReason == CloseReason.UserClosing) e.Cancel =

我读过一些关于禁用Alt+F4的文章,到目前为止,我已经让它部分正常工作了

目前,我在我的主窗体上有这个,它做得很好,只允许在需要时关闭窗体

private void Alerter_FormClosing(object sender, FormClosingEventArgs e)
{
    if (_altF4Pressed)
    {
        if (e.CloseReason == CloseReason.UserClosing)
            e.Cancel = true;
        _altF4Pressed = false;
    }
}

private void Alerter_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Alt && e.KeyCode == Keys.F4)
        _altF4Pressed = true;
}
但问题是,当我显示对话框时,这不起作用。 表单仍然在Alt+F4上关闭,这是必须的,因为它为用户提供了一种绕过流程和过程的方法

目前,我的警报表单称为使用

    public static DialogResult show(string param)
    {
        thisParam = param;

        using (Alerter alert = new Alerter())
        {
            if (alert.ShowDialog() == DialogResult.OK) { return DialogResult.OK; }
            else { return DialogResult.Cancel; }
        }
    }
我的主要形式称之为使用它

Alerter.show(rxLine);
如何防止新表单被关闭,除非我特别告诉它?

从,您可以这样做:

protected override CreateParams CreateParams
{
   get
   {
      const int CS_NOCLOSE = 0x200;

      CreateParams cp = base.CreateParams;
      cp.ClassStyle |= CS_NOCLOSE;
      return cp;
   }
}

无需在FormClosing事件中跟踪键或取消关闭。此外,角落中的“关闭”按钮将被禁用,但您仍然可以使用
this.close()关闭表单

你到底为什么想要这个?ALT+F4是最后的选择。你在写什么,一个病毒?必须同意@ofstream的观点:在任何情况下,对你的用户这样做都是不合适的。如果他们想让你的对话不碍事,他们会设法让它不碍事。如果您提供给他们关闭的功能(以及“确定吗?”对话框),那么他们将使用该功能。如果你不这样做,那么他们会生你的气,然后使用任务管理器终止进程。虽然我可以理解这种谨慎。。不,我不是在写病毒。。。事实上远非如此。我正在编写对现有定制应用程序的升级,其目的是通知最终用户产品召回并强制确认通知。然后,签字人负责采取召回行动。。我们还制造用户使用的机器。。并锁定不必要的访问,以防止数据库等关键流程被篡改。