[英]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. MSDN电源管理文档说明,当显示器打开或关闭时,您可以获得通知。 Basically, you register for the power broadcast messages, and then you'll get a
WM_POWERBROADCAST
message whenever there's a change. 基本上,您注册的是超级广播消息,然后每当有更改时,您都会收到
WM_POWERBROADCAST
消息。 There are several different GUIDs for monitoring the display state, depending on which version of Windows you use. 有几种不同的GUID用于监视显示状态,具体取决于您使用的Windows版本。
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.) (根据要定位的Windows版本, GUID还有其他选择 。)
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.