c# - 向 Windows.Forms 退出按钮添加功能?

标签 c# winforms button exit

在 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/

相关文章:

c# - 不存储引用时更大的 .maxstack?

c# - 减小 winforms 自定义控件周围的选择框的大小

c# - 当 App 连接到 Internet 时表单挂起

c++ - 在 VC++ 中通过 C++ 代码访问 GUI 组件

C#获取XML属性

c# - Rx - 取消订阅事件

c# - 枚举函数错误

java - 安装后按钮不会显示在应用程序中

java - 无法从按钮监听器接收数据

flash AS3自定义按钮翻转(windows 7风格)