是否可以在禁用 "close" 按钮的同时仍然能够从其他地方关闭?
Is it possible to disable the "close" button while still being able to close from elsewhere?
我有一个 winforms 应用程序,我想要程序右上角的关闭按钮来最小化程序。
我已经能够通过使用表单的 FormClosing
事件来实现这一点:
this.Hide();
e.Cancel = true;
但不幸的是,这也停止了我在表单上放置的任何其他关闭按钮。
有没有办法只停止右上角的默认按钮,但仍然可以在其他地方关闭表单?
使用它来禁用右上角表单的关闭按钮。
public partial class Form1 : Form
{
const int MfByposition = 0x400;
[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);
public Form1()
{
InitializeComponent();
var hMenu = GetSystemMenu(Handle, false);
var menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MfByposition);
...
}
}
这是一个简单的布尔值示例:
bool ExitApplication = false;
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
switch(ExitApplication)
{
case false:
this.Hide();
e.Cancel = true;
break;
case true:
break;
}
}
因此,当您想要关闭应用程序时,只需将 ExitApplication 设置为 true。
另一种禁用右上角"X"的方法:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
const int CS_NOCLOSE = 0x200;
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}
}
仍然可以使用 this.Close();
以编程方式关闭表单。
我有一个 winforms 应用程序,我想要程序右上角的关闭按钮来最小化程序。
我已经能够通过使用表单的 FormClosing
事件来实现这一点:
this.Hide();
e.Cancel = true;
但不幸的是,这也停止了我在表单上放置的任何其他关闭按钮。
有没有办法只停止右上角的默认按钮,但仍然可以在其他地方关闭表单?
使用它来禁用右上角表单的关闭按钮。
public partial class Form1 : Form
{
const int MfByposition = 0x400;
[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);
public Form1()
{
InitializeComponent();
var hMenu = GetSystemMenu(Handle, false);
var menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MfByposition);
...
}
}
这是一个简单的布尔值示例:
bool ExitApplication = false;
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
switch(ExitApplication)
{
case false:
this.Hide();
e.Cancel = true;
break;
case true:
break;
}
}
因此,当您想要关闭应用程序时,只需将 ExitApplication 设置为 true。
另一种禁用右上角"X"的方法:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
const int CS_NOCLOSE = 0x200;
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}
}
仍然可以使用 this.Close();
以编程方式关闭表单。