C# 禁用 Windows 窗体上的退出按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684289/
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
Disabling the exit button on a windows form?
提问by Kredns
Is there a way to disable the exit button on a windows form without having to import the some external .dll's? I disable the exit button by importing dll's using the following code but I don't like it. Is there a simpler (built-in) way?
有没有办法禁用 Windows 窗体上的退出按钮而不必导入一些外部 .dll 的?我通过使用以下代码导入 dll 来禁用退出按钮,但我不喜欢它。有没有更简单的(内置)方式?
public Form1()
{
InitializeComponent();
hMenu = GetSystemMenu(this.Handle, false);
}
private const uint SC_CLOSE = 0xf060;
private const uint MF_GRAYED = 0x01;
private IntPtr hMenu;
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern int EnableMenuItem(IntPtr hMenu, uint wIDEnableItem, uint wEnable);
// handle the form's Paint and Resize events
private void Form1_Paint(object sender, PaintEventArgs e)
{
EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);
}
private void Form1_Resize(object sender, EventArgs e)
{
EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);
}
采纳答案by Jim H.
A little poking around found this handy helper class:
稍微浏览一下就发现了这个方便的助手类:
Disable Close Button and Prevent Form Being Moved (C# version)
It actually does more than what you're looking for, but essentially does it very nearly the same way you do in your sample code. The helper class hooks into the load/resize events for you so you don't have to remember to do it yourself.
它实际上做的比您要查找的要多,但基本上与您在示例代码中所做的几乎相同。helper 类会为您挂钩加载/调整大小事件,因此您不必记住自己做。
回答by Ovidiu Pacurar
Catch the FormClosing event and cancel it in the arguments.
捕获 FormClosing 事件并在参数中取消它。
回答by Judah Gabriel Himango
Yes.
是的。
Setting the form.ControlBox = false will hide the close button. Although, it will also hide the minimize and maximize button.
设置 form.ControlBox = false 将隐藏关闭按钮。虽然,它也会隐藏最小化和最大化按钮。
You can also set form.FormBorderStyle = FormBorderStyle.None, which will hide the whole title bar.
您还可以设置form.FormBorderStyle = FormBorderStyle.None,这将隐藏整个标题栏。
If you want to show the X button but just stop the form from closing, override OnClosing and set the e.Cancel property to true.
如果您想显示 X 按钮但只是停止关闭表单,请覆盖 OnClosing 并将 e.Cancel 属性设置为 true。
回答by Mike Bannister
Using visual studio select the form go to the properties and set the ControlBox property to false or try this.ControlBox = false;
or frmMainForm.ControlBox = false;
使用 Visual Studio 选择表单转到属性并将 ControlBox 属性设置为 false 或尝试this.ControlBox = false;
或frmMainForm.ControlBox = false;
回答by Bevan
You can't easily disablethe exit button (the one in the top right that closes the form).
您无法轻松禁用退出按钮(右上角的关闭表单的按钮)。
You can, however, hide the button completely, by setting the ControlBox
property to false.
但是,您可以通过将该ControlBox
属性设置为 false 来完全隐藏按钮。
ControlBox
can be turned on and off at any time, so you can use this if you want to dynamically allow closing at some times, and not at others.
ControlBox
可以随时打开和关闭,因此如果您想在某些时候动态地允许关闭,而不是在其他时候,则可以使用它。
Alternatively, you can handle the FormClosing event and cancel the close if you choose.
或者,您可以选择处理 FormClosing 事件并取消关闭。
Here's a demo.
这是一个演示。
Create a new Windows Forms project.
创建一个新的 Windows 窗体项目。
Drop a CheckBox control on the form, with Text "ControlBox". Hook up its Click event to this:
在窗体上放置一个 CheckBox 控件,文本为“ControlBox”。将它的 Click 事件连接到这个:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
ControlBox = checkBox1.Checked;
}
Then, drop a second CheckBox control on the form with Text "Cancel Close". Hook up the FormClosing event of the form to this:
然后,使用文本“取消关闭”将第二个 CheckBox 控件拖放到窗体上。将表单的 FormClosing 事件连接到这个:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = checkBox2.Checked;
}
Run the application and start playing around with the checkboxes. You'll soon see how things work.
运行应用程序并开始使用复选框。你很快就会看到事情是如何运作的。
回答by Saran N V
To disable the close button on the form just write the below code on the form closing event.
要禁用表单上的关闭按钮,只需在表单关闭事件上编写以下代码。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
回答by Barry Guvenkaya
Disabling the button is possible without importing dlls. ControlBox = false
causes minimise and maximise buttons and the border to disappear as well and it does not disable the close 'X' button - it hides. This solution disables it:
无需导入 dll 即可禁用该按钮。ControlBox = false
导致最小化和最大化按钮以及边框消失,并且它不会禁用关闭“X”按钮 - 它隐藏。此解决方案禁用它:
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
return myCp;
}
}