且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows 进入睡眠模式时的通知

更新时间:2022-06-19 23:21:00

如果您使用的是托管代码,那么这会在 SystemEvents.PowerModeChanged 事件中公开.

If you're using managed code then this is exposed in the SystemEvents.PowerModeChanged event.

SystemEvents.PowerModeChanged += OnPowerModeChanged;

private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) {
  if (e.Mode == PowerModes.Suspend) {
    // Going to sleep
  }
}

如果您使用本机代码,那么您希望在 WindowProc 处理程序中监听 WM_POWERBROADCAST 消息.

If you're using native code then you want to listen for the WM_POWERBROADCAST message in your WindowProc handler.

LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  if (WM_POWERBROADCAST == message && PBT_APMSUSPEND == wParam) {
    // Going to sleep
  }
}