我可以使用 C# 禁用表单的“关闭”按钮吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12422619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:13:16  来源:igfitidea点击:

Can I disable the 'close' button of a form using C#?

c#.netwinformsformsinterface

提问by

How can I disable the close button of a form like in the image below? (the image below show a MessageBoxwindow)

如何禁用如下图所示的表单的关闭按钮?(下图显示了一个MessageBox窗口)

enter image description here

在此处输入图片说明

The MessageBoxabove was generated by me! I want to disable the close button of a normal form.

MessageBox被我产生以上!我想禁用普通表单的关闭按钮。

采纳答案by dsgriffin

You handle the Closingevent (and not the Closed event) of the Form.

您处理表单的Closing事件(而不是 Closed 事件)。

And then you use e.CloseReason to decide if you really want to block it (UserClose) or not (TaskManager Close).

然后你使用 e.CloseReason 来决定你是否真的想要阻止它(UserClose)或不(TaskManager Close)。

Also, there is small example Disabling Close Button on Formson codeproject.

此外,还有一个小示例Disabling Close Button on Formson codeproject

回答by Peru

You should override the CreateParamsfunction derived from System.Windows.Forms.Form

您应该覆盖CreateParams派生自的函数System.Windows.Forms.Form

And change the Class Style

并改变课堂风格

myCp.ClassStyle = 0x200;

回答by Derek W

Right click on the window in question, then click Properties. Under Properties click Events. Double click on the FormClosingevent.

右键单击有问题的窗口,然后单击“属性”。在属性下单击事件。双击FormClosing事件。

The following code will be generated by Windows Form Designer:

Windows 窗体设计器将生成以下代码:

private void myWindow_FormClosing(object sender, FormClosingEventArgs e)
{

}

Simply update the code to look like this (add e.Cancel = true;):

只需将代码更新为如下所示(添加e.Cancel = true;):

private void myWindow_FormClosing(object sender, FormClosingEventArgs e)
{
     e.Cancel = true;
}

You're done!

你完成了!

Alternatively, if you want to disable the close, maximize, and minimize buttons of the window:

或者,如果您想禁用窗口的关闭、最大化和最小化按钮:

You can right click the window in question, then click Properties. Under Properties set the ControlBoxproperty to false.

您可以右键单击有问题的窗口,然后单击“属性”。在“属性”下,将属性设置ControlBoxfalse

回答by Alex Jorgenson

If you are working with an MDI child window and disabling the close button during creation of the window is ruled out (i.e. you want to disable it at certain times during the form's life) none of the previously proposed solutions will work1.

如果您正在使用 MDI 子窗口并且在创建窗口期间禁用关闭按钮被排除(即,您希望在窗体生命周期中的某些时间禁用它),则先前提出的解决方案都将不起作用1。

Instead we must use the following code:

相反,我们必须使用以下代码:

[DllImport("user32")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("user32")]
public static extern bool EnableMenuItem(IntPtr hMenu, uint itemId, uint uEnable);

public static void DisableCloseButton(this Form form)
{
    // The 1 parameter means to gray out. 0xF060 is SC_CLOSE.
    EnableMenuItem(GetSystemMenu(form.Handle, false), 0xF060, 1);
}

public static void EnableCloseButton(this Form form)
{
    // The zero parameter means to enable. 0xF060 is SC_CLOSE.
    EnableMenuItem(GetSystemMenu(form.Handle, false), 0xF060, 0);
}

1 You can set ControlBox = falseif you do not want any buttons, but that is not what the question is asking.

1ControlBox = false如果您不需要任何按钮,您可以设置,但这不是问题所在。

回答by Mac Lee

 Closing += (s, eventArgs) =>
        {
            DialogResult = DialogResult.None; //means that dialog would continue running
        };