WPF捕捉Windows关机事件

时间:2014-06-27 20:29:33   收藏:0   阅读:564
bubuko.com,布布扣
1    private const int SC_SCREENSAVE = 0xF140;
2         private const int WM_QUERYENDSESSION = 0x0011;
3         private bool IsScreenSave = false;
4         private bool IsWindowStopClosing = false;
变量


不废话,直接贴代码,构造函数中

public XX{

 

bubuko.com,布布扣
1    // 获得窗口句柄
2                 IntPtr hwnd = new WindowInteropHelper(this).Handle;
3                 // 在Win32窗口中显示WPF的内容
4                 HwndSource src = HwndSource.FromHwnd(hwnd);
5                 //接收窗口消息的处理程序实现(基于 System.Windows.Interop.HwndSourceHook 委托)
6                 src.AddHook(new HwndSourceHook(WndProc));
Register

 

}

 

bubuko.com,布布扣
 1     /// <summary>
 2         /// 重新WndProc
 3         /// </summary>
 4         /// <param name="hwnd">窗口句柄</param>
 5         /// <param name="msg">消息ID</param>
 6         /// <param name="wParam">消息的 wParam 值</param>
 7         /// <param name="lParam">消息的 lParam 值</param>
 8         /// <param name="handled">指示该消息是否已处理的值。如果该消息已处理,请将值设置为 true;否则请将其设置为 false</param>
 9         /// <returns></returns>
10         private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
11         {
12             if (msg == WM_QUERYENDSESSION)
13             {
14                 using (var wcf = new KeFuServicesClient())
15                 {
16                     wcf.UpdateUserState(MainPanelWindow.ClientServiceID, UserStates.UnLine);
17                 }
18             }
19             return IntPtr.Zero;
20         }
捕捉操作系统关机事件

 .NET技术交流群 199281001 .欢迎加入。

WPF捕捉Windows关机事件,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!