如果系统处于活动状态,如何检查C ++?

我正在编写代码,只有在PC上没有任何人为活动的时候才需要运行,就像屏保运行一样。 任何build议如何在Windows下的c + +做到这一点?

@talnicolas,只是为了使用未使用的资源,有多less人离开电脑,但他们在另一个地方?

您可以使用GetLastInputInfo来检查用户闲置了多长时间(不是围绕鼠标移动或在键盘上键入内容)和SystemParametersInfo来检查屏幕保护程序是否处于活动状态。

 #define WINDOWS_LEAN_AND_MEAN #include <windows.h> #include <iostream> // do something after 10 minutes of user inactivity static const unsigned int idle_milliseconds = 60*10*1000; // wait at least an hour between two runs static const unsigned int interval = 60*60*1000; int main() { LASTINPUTINFO last_input; BOOL screensaver_active; // main loop to check if user has been idle long enough for (;;) { if ( !GetLastInputInfo(&last_input) || !SystemParametersInfo(SPI_GETSCREENSAVERACTIVE, 0, &screensaver_active, 0)) { std::cerr << "WinAPI failed!" << std::endl; return ERROR_FAILURE; } if (last_input.dwTime < idle_milliseconds && !screensaver_active) { // user hasn't been idle for long enough // AND no screensaver is running Sleep(1000); continue; } // user has been idle at least 10 minutes do_something(); // done. Wait before doing the next loop. Sleep(interval); } } 

请注意,我在Linux机器上编写了这个代码,所以我无法测试它。

使用SystemParametersInfo(…)

  private const int SPI_GETSCREENSAVERACTIVE = 16; SystemParametersInfo( SPI_GETSCREENSAVERACTIVE, 0, ref isActive, 0 ); return isActive; 

参考: http : //www.codeproject.com/KB/cs/ScreenSaverControl.aspx

这只是在Windows 8.1上测试的Niklas B.的一个小的更新。

变更的描述如下:

  • LASTINPUTINFO已初始化cbSize

  • 而不是检查SPI_GETSCREENSAVERACTIVESPI_GETSCREENSAVERRUNNING被选中

  • 计算idle_time ,并与idle_milliseconds进行比较

  • 用户仅在更改时通知,每500ms执行一次检查

  • 由于Windows可以在4802之后立即触发事件ID 4803,所以可以确定屏幕保护程序仍在用户idle_time ,看到我遇到的问题

#define start

 #include <windows.h> #include <iostream> // do something after 10 minutes of user inactivity static const unsigned int idle_milliseconds = 60*10*1000; int main() { LASTINPUTINFO last_input; // without setting cbSize GetLastError() returns the parameter is incorrect last_input.cbSize = sizeof(last_input); BOOL screensaver_running; DWORD idle_time; bool screensaverOn = false; // main loop to check if user has been idle long enough for (;;) { if ( !GetLastInputInfo( &last_input ) || !SystemParametersInfo( SPI_GETSCREENSAVERRUNNING, 0, &screensaver_running, 0 ) ) { std::cerr << "WinAPI failed!" << std::endl; return ERROR_FAILURE; } // calculate idle time idle_time = GetTickCount() - last_input.dwTime; if ( idle_time > this->user_idle_time && TRUE == screensaver_running ) { if ( !screensaverOn ) { // screensaver is running screensaverOn = true; notify( true ); } } else if ( idle_time < this->user_idle_time && screensaverOn ) { // screensaver is not running screensaverOn = false; notify( false ); } // wait 500ms before next loop Sleep( 500 ); } } 

检查在这个看起来相似的答案 。

然而,你需要确定是否是“不活跃”的意思。 这改变了你的问题的范围。

另外检查这个代码

 hdesk = OpenDesktop(TEXT("Screen-saver"), 0, FALSE, DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS); if (hdesk) { EnumDesktopWindows (hdesk, (WNDENUMPROC)KillScreenSaverFunc, 0); CloseDesktop (hdesk); } // ---------------------------------------------------------------- BOOL CALLBACK KillScreenSaverFunc (HWND hwnd, LPARAM lParam) { PostMessage(hwnd, WM_CLOSE, 0, 0); return TRUE; } 

从这里 。