在 C#.NET 4.0 中编程是我最近的爱好,我想知道如何向标准 Windows.Forms 退出按钮(表单右上角的红色 X)添加功能。
我找到了一种禁用按钮的方法,但由于我认为它会影响用户体验,所以我想改为连接一些功能。
如何禁用退出按钮:
#region items to disable quit-button
const int MF_BYPOSITION = 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);
#endregion
...
private void DatabaseEditor_Load(object sender, EventArgs e)
{
this.graphTableAdapter.Fill(this.diagramDBDataSet.Graph);
this.intervalTableAdapter.Fill(this.diagramDBDataSet.Interval);
// Disable quit-button on load
IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
}
但是在应用程序使用标准退出按钮退出之前,我究竟要如何附加一个方法。我想在退出 Windows 窗体之前对列表进行 XmlSerialize。
最佳答案
如果你想在窗体关闭前写代码,使用FormClosing事件
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
关于c# - 向 Windows.Forms 退出按钮添加功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3163885/