有些时候当程序退出时,可能需要做一些善后处理工作。.Net框架为我们提供了非常丰富及方便的窗体关闭检测手段,我们只要捕获这些相关事件,然后对应处理即可,从而达到不同原因关闭窗体采取不同操作的目的。
首先,创建一个默认的Windows Form. Application,我们需要重载Form基类的OnClosing事件,然后获取窗体关闭事件的参数信息,即FormClosingEventArgs类的实例,根据关闭窗体的不同原因,即FormClosingEventArgs类的CloseReason属性,提示用户作出相应处理。Form1.cs的全部源码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static bool ShowMessage(string s)
{
DialogResult result = MessageBox.Show(s + "\n\n确定要关闭窗体吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
return result == DialogResult.No;
}
protected override void OnClosing(CancelEventArgs e)
{
FormClosingEventArgs fce = e as FormClosingEventArgs;
if (fce != null)
{
switch (fce.CloseReason)
{
case CloseReason.ApplicationExitCall:
e.Cancel = Form1.ShowMessage("程序正准备退出!");
break;
case CloseReason.FormOwnerClosing:
e.Cancel = Form1.ShowMessage("主窗体正在关闭!");
break;
case CloseReason.MdiFormClosing:
e.Cancel = Form1.ShowMessage("MDI主窗体正在试图关闭!");
break;
case CloseReason.None:
e.Cancel = Form1.ShowMessage("正在关闭窗体,但原因未知!");
break;
case CloseReason.TaskManagerClosing:
e.Cancel = Form1.ShowMessage("任务管理器正在试图关闭窗体并终止程序!");
break;
case CloseReason.UserClosing:
e.Cancel = Form1.ShowMessage("用户正在试图关闭窗体!");
break;
case CloseReason.WindowsShutDown:
e.Cancel = Form1.ShowMessage("Windows 正在关闭,并尝试关闭窗体!");
break;
}
}
base.OnClosing(e);
}
}
}
在事件结尾不要忘记添加对基类的OnClosing()的调用,以保证类的完整性!
更多关于.Net下侦测窗体关闭及关闭原因的内容,请参考用于Visual Studio 2008 的MSDN:
http://msdn.microsoft.com/zh-cn/library/system.windows.window.onclosing.aspx
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.formclosingeventargs.aspx
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.closereason.aspx
该示例在Windows XP SP3 + Visual Studio 2008 SP1 下编译调试通过。
首先,创建一个默认的Windows Form. Application,我们需要重载Form基类的OnClosing事件,然后获取窗体关闭事件的参数信息,即FormClosingEventArgs类的实例,根据关闭窗体的不同原因,即FormClosingEventArgs类的CloseReason属性,提示用户作出相应处理。Form1.cs的全部源码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static bool ShowMessage(string s)
{
DialogResult result = MessageBox.Show(s + "\n\n确定要关闭窗体吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
return result == DialogResult.No;
}
protected override void OnClosing(CancelEventArgs e)
{
FormClosingEventArgs fce = e as FormClosingEventArgs;
if (fce != null)
{
switch (fce.CloseReason)
{
case CloseReason.ApplicationExitCall:
e.Cancel = Form1.ShowMessage("程序正准备退出!");
break;
case CloseReason.FormOwnerClosing:
e.Cancel = Form1.ShowMessage("主窗体正在关闭!");
break;
case CloseReason.MdiFormClosing:
e.Cancel = Form1.ShowMessage("MDI主窗体正在试图关闭!");
break;
case CloseReason.None:
e.Cancel = Form1.ShowMessage("正在关闭窗体,但原因未知!");
break;
case CloseReason.TaskManagerClosing:
e.Cancel = Form1.ShowMessage("任务管理器正在试图关闭窗体并终止程序!");
break;
case CloseReason.UserClosing:
e.Cancel = Form1.ShowMessage("用户正在试图关闭窗体!");
break;
case CloseReason.WindowsShutDown:
e.Cancel = Form1.ShowMessage("Windows 正在关闭,并尝试关闭窗体!");
break;
}
}
base.OnClosing(e);
}
}
}
在事件结尾不要忘记添加对基类的OnClosing()的调用,以保证类的完整性!
更多关于.Net下侦测窗体关闭及关闭原因的内容,请参考用于Visual Studio 2008 的MSDN:
http://msdn.microsoft.com/zh-cn/library/system.windows.window.onclosing.aspx
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.formclosingeventargs.aspx
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.closereason.aspx
该示例在Windows XP SP3 + Visual Studio 2008 SP1 下编译调试通过。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14325734/viewspace-539740/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/14325734/viewspace-539740/