我正在尝试创建 Windows 屏幕保护程序,它根据人脸检测来打开和关闭显示器。这是基本代码(c++ 和 winapi):
#define TIMER 1010
unsigned int FREQUENCY_OF_CHECK = 5000;
LRESULT WINAPI ScreenSaverProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
if(!fChildPreview)
{
switch(message)
{
case WM_CREATE:
//start timer
SetTimer(hwnd, TIMER, FREQUENCY_OF_CHECK, NULL);
//create transparent see thru layer
SetWindowLong(
hwnd,
GWL_EXSTYLE,
GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED
);
break;
case WM_DESTROY:
SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
KillTimer(hwnd, TIMER);
break;
case WM_TIMER:
//separate process detects face and stores detection into registry
if(!ProcessRunning("capture.exe")){
ShellExecute(
NULL,
"open",
"C:/camsaver/capture.exe",
"",
"",
SW_SHOWNOACTIVATE);
}
//load detection from registry and then turn monitor on/off
bool face;
readFaceFromRegistry(face);
if (face){
//turn monitor on
SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
}
else {
//turn monitor off
SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
}
break;
default:
return DefScreenSaverProc(hwnd, message, wParam, lParam);
}
return 0;
}
}
如果屏幕保护程序在未检测到人脸时自行运行,它只会关闭显示器并停止执行任何其他操作。
我希望它继续运行并返回屏幕检测到人脸时亮起。就像在预览模式下运行时一样。
我的猜测是 SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
这行所做的事情比我意识到的要多。
最佳答案
显然在 Win7 中 SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
不仅关闭屏幕而且关闭屏幕保护程序(甚至没有麻烦至少先发送一条 destoy 消息)。没有简单的方法可以解决这个问题。
引用:我找到this它还会导致 msdn 上的一些其他线程。 (由于我的声誉,我现在无法链接...)
当我在处理时,上面的代码也不适用于 Win8,因为 SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
Win8不亮屏。然而,这应该很容易解决,如 this C# workaround .
关于c++ - Windows C++ 屏幕保护程序无法重新打开屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574584/