本文介绍了如何知道您的计算机在屏幕保护程序或监视器/屏幕关闭的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Windows 7和VC ++。业务是知道我的系统被设置到屏幕保护模式或监视屏幕关闭的秒数。为了实现这一点,我正在尝试捕获事件WM_SYSCOMMAND和SC_SCREENSAVE,SC_MONITORPOWER。所以我在Visual Studio 2008中创建了一个Win32项目,我收到了WndProc函数中的事件: LRESULT CALLBACK WndProc(HWND hWnd,UINT消息,WPARAM wParam,LPARAM lParam) { int wmId,wmEvent; PAINTSTRUCT ps; HDC hdc; switch (消息) { case WM_SYSCOMMAND: { switch (LOWORD(wParam)) { case SC_SCREENSAVE: { FILE * fl = fopen( this_is_a_event_test。 txt, a); fputs( SC_SCREENSAVE \ n,fl); fclose(fl); } break ; case SC_MONITORPOWER: { FILE * fl = fopen( this_is_a_event_test.txt, a); fputs( SC_MONITORPOWER \ n,fl); fclose(fl); } break ; 默认: {} } } 断裂; } } 当对话框在前台时,它可以正常工作,但在后台(或者如果我发表评论) ShowWindow函数)它只有在我手动发送事件时才有效: SendMessage(HWND_BROADCAST,WM_SYSCOMMAND,SC_SCREENSAVE,(LPARAM) 2 ); 或 SendMessage(HWND_BROADCAST ,WM_SYSCOMMAND,SC_MONITORPOWER,(LPARAM) 2 ); 所以,当系统电源配置在2分钟不活动后设置屏幕保护程序时,它不起作用,自动监视器屏幕关闭时也是如此。这是我想要的真实的东西,知道系统何时关闭屏幕或者使用后台监控程序设置屏幕保护程序。 我也尝试过使用钩子extern dll的事件。我按照这个例子 Hooks和DLLs 在CALLBACK msghook()函数中添加了相同的开关代码以上在WndProc。它甚至不能使用SendMessage。 经过几天坚持这个问题,在互联网上搜索,论坛......我不知道还有什么我可以做的。任何人都可以帮助我吗?解决方案 您好@analogx。你是对的,我没有正确使用挂钩,但这种情况很少见。首先,关于setWindowsHookEx函数,我已经读过WH_CALLWNDPROC或WH_SYSMSGFILTER必须用来获取WM_SYSCOMMAND发送的消息,然后得到SC_SCREENSAVE wParam。在这种情况下,我不知道为什么,也许我错了,但这似乎不是真的。 使用每条可能的消息后,SetWindowsHookEx ,我意识到WH_GETMESSAGE是唯一发送SC_SCREENSAVE或SC_MONITORPOWER wParam的人,至少在Windows 7的这个钩子示例中。 HHOOK钩; HHOOK hook = SetWindowsHookEx( WH_GETMESSAGE ,(HOOKPROC)msghook, hInst, 0 ); 其次,只有在使用LPMSG时,才会监听钩子函数中捕获的每条消息,WM_SYSCOMMAND。我还读到必须将wParam组合到0xFFF0进行比较。但是wParam& 0xFFF0 == SC_SCREENSAVE无法正常工作,wParam == SC_SCREEN也没有。在这种情况下,唯一的方法是使用LPMSG。 静态 LRESULT CALLBACK msghook(UINT代码,WPARAM wParam,LPARAM lParam) { if (code< 0 ) { CallNextHookEx(hook,code,wParam,lParam); return 0 ; } LPMSG msg =(LPMSG)lParam; if (msg-> message == WM_SYSCOMMAND) { if (msg-> wParam == SC_SCREENSAVE) { MessageBoxA(NULL,L SC_SCREENSAVE,L SC_SCREENSAVE, MB_OK); } if (msg-> wParam == SC_MONITORPOWER) { MessageBoxA(NULL ,L SC_MONITORPOWER,L SC_MONITORPOWER,MB_OK); } } return CallNextHookEx(hook,nCode,wParam,lParam); } 使用FILE测试事件是一个非常糟糕的主意,我认为使用MessageBox并不是更好但是我不知道如何正确测试。 I''m using Windows 7 and VC++. The business is to know how many seconds my system has been set into screen saver mode or monitor screen off. To achieve this, I''m trying to catch the events WM_SYSCOMMAND and SC_SCREENSAVE, SC_MONITORPOWER. So I have created a Win32 project in Visual Studio 2008 and I''m receiving the events in WndProc function: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_SYSCOMMAND: { switch (LOWORD(wParam)) { case SC_SCREENSAVE: { FILE *fl = fopen("this_is_a_event_test.txt","a"); fputs("SC_SCREENSAVE\n",fl); fclose(fl); } break; case SC_MONITORPOWER: { FILE *fl = fopen("this_is_a_event_test.txt","a"); fputs("SC_MONITORPOWER\n",fl); fclose(fl); } break; default: { } } } break; } }It works fine when dialog is in foreground, but in background (or if I comment ShowWindow function) it only works if I manually send the events: SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_SCREENSAVE, (LPARAM)2); or SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);So, it is not working when system power configuration sets the screen saver after 2 minutes of inactivity, and the same thing with automatic monitor screen off. Thats the real thing I want, know when the system is turning off the screen or setting the screen saver with a background monitoring program.I have also tried to use hook events with extern dll. I have followed this example Hooks and DLLs adding in the CALLBACK msghook() function the same switch code above in WndProc. It doesn''t work even using the SendMessage.After several days stuck with this issue, searching in the Internet, forums... I don''t know what else I can do. Can anyone help me? 解决方案 这篇关于如何知道您的计算机在屏幕保护程序或监视器/屏幕关闭的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 21:55