如何知道您的计算机在屏幕保护程序或监视器/屏幕关闭的时间? [英] How to know how many time your computer has been in screen saver or monitor/screen off?

查看:180
本文介绍了如何知道您的计算机在屏幕保护程序或监视器/屏幕关闭的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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。



经过几天坚持这个问题,在互联网上搜索,论坛......我不知道还有什么我可以做的。任何人都可以帮助我吗?

解决方案

你试过正确使用钩子,因为它有效,我做了类似的代码,我的代码跟踪窗口的创建,作为一个参考我使用SPY程序来自本网站的Dor ALan,我使用他的程序作为开发我的代码的基础,你也可以在这个链接看看我的代码,

http://www.codeproject.com/Questions/523472/Handlingpluswh_shell-27spluswindowplusdestroyplusm

当屏幕保护程序变为oFF或你的监视器开启时,也尝试写入文件,因为我当你尝试使用你的代码编写时,它会引发一个事件并唤醒系统...

随意问你是否有任何疑问


< blockquote>您好@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?

解决方案

have u tried properly using hooks,because it works,i have done similar kind of code where my code tracks creation of window,as a reference i used SPY program By Dor ALan from this site,i used his program as a base for developing my code,you can also have a look at my code at this link,
http://www.codeproject.com/Questions/523472/Handlingpluswh_shell-27spluswindowplusdestroyplusm
Also try writing to file when the screen saver turns oFF or your moitor turns on,cause i''m guessing when you try to write using your code it causes a event and wakes up the system...
Feel free to ask if you hav any doubts


Hi @analogx. You are right, I were not using hooks properly, but it has been rare. Firstly, about setWindowsHookEx function, I have read WH_CALLWNDPROC or WH_SYSMSGFILTER must be used to get WM_SYSCOMMAND sent messages, and then get SC_SCREENSAVE wParam. In this case, I don''t know why and maybe I''m wrong, but thats seems not to be true.

After use every possible message to SetWindowsHookEx, I realised WH_GETMESSAGE is the only one who sends SC_SCREENSAVE or SC_MONITORPOWER wParam, at least in this hook example in Windows 7.

HHOOK hook;
HHOOK hook = SetWindowsHookEx(WH_GETMESSAGE,
                (HOOKPROC)msghook,
                hInst,
                0);



Secondly, listening for every message caught in hook function, WM_SYSCOMMAND were appeared only if LPMSG were used. I have read also that wParam must to be combined to 0xFFF0 to be compared. But wParam & 0xFFF0 == SC_SCREENSAVE didn''t work and wParam == SC_SCREEN neither. In this case the only way is using LPMSG for both.

static LRESULT CALLBACK msghook(UINT code, 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);
}



And using FILE to test the events was a very bad idea, I think using MessageBox is not much better but I don''t know how to test ir correctly.


这篇关于如何知道您的计算机在屏幕保护程序或监视器/屏幕关闭的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆