我正在尝试创建根据面部检测打开和关闭监视器的 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);
所做的事情比我意识到的要多。