C# 捕获关机事件方法
2016-06-22 19:55
369 查看
这篇文章是在互联网搜索到的,但是很多文章都没有给 WM_QUERYENDSESSION赋值这句话,所以重新整理了一下
/// <summary>
/// 窗口过程的回调函数
/// </summary>
///<param name="m">
private const int WM_QUERYENDSESSION = 0x0011;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
//此消息在OnFormClosing之前
case WindowsMessage.WM_QUERYENDSESSION:
//MessageBox.Show(WndProc.WM_QUERYENDSESSION.我要阻止系统关闭!);
//this.Close();
//this.Dispose();
//Application.Exit();
m.Result = (IntPtr)1; //阻止Windows注销、关机或重启
break;
default:
break;
}
base.WndProc(ref m);
}
方法二:
protected override void OnFormClosing(FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.ApplicationExitCall:
e.Cancel = true;
MessageBox.Show(拦截关闭要求事件!);
break;
case CloseReason.FormOwnerClosing:
e.Cancel = true;
MessageBox.Show(拦截自身关闭事件!);
break;
case CloseReason.MdiFormClosing:
e.Cancel = true;
MessageBox.Show(拦截MDI窗体关闭事件!);
break;
case CloseReason.None:
break;
case CloseReason.TaskManagerClosing:
e.Cancel = true;
MessageBox.Show(拦截任务管理器关闭事件!);
break;
case CloseReason.UserClosing:
//注销或关机会触发此事件;
//MessageBox.Show(拦截用户关闭事件!);
e.Cancel = false;
break;
case CloseReason.WindowsShutDown:
e.Cancel = true;
MessageBox.Show(拦截关机事件!);
break;
default:
break;
}
base.OnFormClosing(e);
}
方法三:
//当用户试图注销或关闭系统时发生。
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
//下面是系统注销或关闭事件处理程序,
private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (MessageBox.Show(this, 是否允许系统注销!, 系统提示, MessageBoxButtons.YesNo) != DialogResult.Yes)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
SessionEndReasons reason = e.Reason;
switch (reason)
{
case SessionEndReasons.Logoff:
MessageBox.Show(用户正在注销。操作系统继续运行,但启动此应用程序的用户正在注销。);
break;
case SessionEndReasons.SystemShutdown:
MessageBox.Show(操作系统正在关闭。);
break;
}
}
//如果把上面的事件处理程序修改成如下
//private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
// {
// e.Cancel = true;
// }
//那会出现什么情况,你点击开始菜单关机选择注销、关机、或重新启动将会失效,电脑不能正常关机了,进一步的话把程序做成Windows服务,晕,恶作剧?
//SessionEnded事件同上,事件参数类为SessionEndedEventArgs,同SessionEndingEventArgs相比少了Cancel属性,Cancel属性同一些windows下的某些事件差不多,比如Form.Closing事件,Control.Validating事件。
/// <summary>
/// 窗口过程的回调函数
/// </summary>
///<param name="m">
private const int WM_QUERYENDSESSION = 0x0011;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
//此消息在OnFormClosing之前
case WindowsMessage.WM_QUERYENDSESSION:
//MessageBox.Show(WndProc.WM_QUERYENDSESSION.我要阻止系统关闭!);
//this.Close();
//this.Dispose();
//Application.Exit();
m.Result = (IntPtr)1; //阻止Windows注销、关机或重启
break;
default:
break;
}
base.WndProc(ref m);
}
方法二:
protected override void OnFormClosing(FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.ApplicationExitCall:
e.Cancel = true;
MessageBox.Show(拦截关闭要求事件!);
break;
case CloseReason.FormOwnerClosing:
e.Cancel = true;
MessageBox.Show(拦截自身关闭事件!);
break;
case CloseReason.MdiFormClosing:
e.Cancel = true;
MessageBox.Show(拦截MDI窗体关闭事件!);
break;
case CloseReason.None:
break;
case CloseReason.TaskManagerClosing:
e.Cancel = true;
MessageBox.Show(拦截任务管理器关闭事件!);
break;
case CloseReason.UserClosing:
//注销或关机会触发此事件;
//MessageBox.Show(拦截用户关闭事件!);
e.Cancel = false;
break;
case CloseReason.WindowsShutDown:
e.Cancel = true;
MessageBox.Show(拦截关机事件!);
break;
default:
break;
}
base.OnFormClosing(e);
}
方法三:
//当用户试图注销或关闭系统时发生。
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
//下面是系统注销或关闭事件处理程序,
private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (MessageBox.Show(this, 是否允许系统注销!, 系统提示, MessageBoxButtons.YesNo) != DialogResult.Yes)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
SessionEndReasons reason = e.Reason;
switch (reason)
{
case SessionEndReasons.Logoff:
MessageBox.Show(用户正在注销。操作系统继续运行,但启动此应用程序的用户正在注销。);
break;
case SessionEndReasons.SystemShutdown:
MessageBox.Show(操作系统正在关闭。);
break;
}
}
//如果把上面的事件处理程序修改成如下
//private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
// {
// e.Cancel = true;
// }
//那会出现什么情况,你点击开始菜单关机选择注销、关机、或重新启动将会失效,电脑不能正常关机了,进一步的话把程序做成Windows服务,晕,恶作剧?
//SessionEnded事件同上,事件参数类为SessionEndedEventArgs,同SessionEndingEventArgs相比少了Cancel属性,Cancel属性同一些windows下的某些事件差不多,比如Form.Closing事件,Control.Validating事件。
相关文章推荐
- C#制作、打包、签名、发布Activex全过程
- C#接口的作用
- c# 发送GET 和 POST请求
- c# 序列化和反序列化
- C# dataTable的linq联合查询,插入到新的dataTable中
- 【C#】关于文件的写入与读取
- C#.NET 大型企业信息化系统 - 防黑客攻击 - SSO系统加固优化经验分享
- C# 通过反射获取实例对象
- C# 将两个不同的dataTable合并到一个自定义的dataTable中
- 关于C#的委托(delegate)的自我理解
- c# Thread
- C#发送邮件代码
- c#软件
- C# 实现像QQ一样隐藏窗体
- 【C#】XML的创建与读取
- c#图片与二进制流相互转换
- c#获取中国城市天气编号代码
- 10 在C#中读取文件
- C# 实现二维数组的排序算法(代码)
- c# 字符串前加@