向Windows.Forms退出按钮添加功能吗? [英] Add functionality to Windows.Forms exit button?

查看:92
本文介绍了向Windows.Forms退出按钮添加功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#.NET 4.0中编程是我的最新爱好,我想知道如何向标准Windows.Forms退出按钮(表单右上角的红色X)添加功能.

Programming in C#.NET 4.0 is my latest passion, and I would like to know how to add functionality to the standard Windows.Forms Exit button (the red X in the upper right corner of the form).

我找到了一种禁用按钮的方法,但是由于我认为它会损害用户的体验,因此我想使用一些功能.

I have found a way to disable the button, but since I think it compromises the user experiance, I would like to hook up some functionalities instead.

如何禁用退出按钮:

    #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.

But how on earth do I attach a method, before the application exits with the standard exit-button. I would like to XmlSerialize a List before exiting the windows form.

推荐答案

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   if(MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
   {
       e.Cancel = true;
   }
}

这篇关于向Windows.Forms退出按钮添加功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆