【发布时间】:2010-07-02 07:15:43
【问题描述】:
在 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 一个列表。
【问题讨论】: