private const int SC_SCREENSAVE = 0xF140;
private const int WM_QUERYENDSESSION = 0x0011;
private bool IsScreenSave = false;
private bool IsWindowStopClosing = false;
变量
不废话,直接贴代码,构造函数中
public XX{
// 获得窗口句柄
IntPtr hwnd = new WindowInteropHelper(this).Handle;
// 在Win32窗口中显示WPF的内容
HwndSource src = HwndSource.FromHwnd(hwnd);
//接收窗口消息的处理程序实现(基于 System.Windows.Interop.HwndSourceHook 委托)
src.AddHook(new HwndSourceHook(WndProc));
Register
}
/// <summary>
/// 重新WndProc
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <param name="msg">消息ID</param>
/// <param name="wParam">消息的 wParam 值</param>
/// <param name="lParam">消息的 lParam 值</param>
/// <param name="handled">指示该消息是否已处理的值。如果该消息已处理,请将值设置为 true;否则请将其设置为 false</param>
/// <returns></returns>
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_QUERYENDSESSION)
{
using (var wcf = new KeFuServicesClient())
{
wcf.UpdateUserState(MainPanelWindow.ClientServiceID, UserStates.UnLine);
}
}
return IntPtr.Zero;
}
捕捉操作系统关机事件
.NET技术交流群 199281001 .欢迎加入。