简体   繁体   中英

How to check PC monitor is turned on or off any tool or event viewer in windows 7

我需要检查在Win7 OS中我的电脑显示器是打开还是关闭

MSDN power management documentation explains that you can get notifications when the monitor is turned on or off. Basically, you register for the power broadcast messages, and then you'll get a WM_POWERBROADCAST message whenever there's a change. There are several different GUIDs for monitoring the display state, depending on which version of Windows you use.

At the beginning of your program (after creating your main windows), you do something like:

HPOWERNOTIFY hPower =
  RegisterPowerSettingNotification(hwndMain, GUID_SESSION_DISPLAY_STATUS, 0);

(There are other choices for the GUID depending on which version of Windows you're targeting.)

Then in your main window procedure:

case WM_POWERBROADCAST:
  if (wParam == PBT_POWERSETTINGCHANGE) {
    const POWERBROADCAST_SETTING *pSetting =
      reinterpret_cast<const POWERBROADCAST_SETTING*>(lParam);
    if (pSetting->PowerSetting == GUID_SESSION_DISPLAY_STATUS) {
      assert(pSetting->DataLength >= sizeof(DWORD));
      DWORD data = *reinterpret_cast<const DWORD*>(&pSetting->Data);
      switch (data) {
        case 0: /* monitor is off */ break;
        case 1: /* monitor is on */ break;
        case 2: /* monitor is dimmed */ break;
        default:  /* ???? */ break;
      }
    }
  }
  break;

If at any point you no longer care about the power notifications, you can unregister:

UnregsisterPowerSettingNotification(hPower);
hPower = NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM