Microsoft KB Archive/238882

{|
 * width="100%"|

HOWTO: Know When Your Screen Saver Starts

 * }

Q238882

-

The information in this article applies to:


 * Microsoft Windows 98
 * Microsoft Windows 95
 * Microsoft Windows NT Server version 4.0
 * Microsoft Windows NT Workstation version 4.0
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Professional

-

SUMMARY
A screen saver will start, pending your choices, whenever there is no mouse or keyboard activity for the current screen saver timeout period. Some applications need to know when the screen saver has started in order to do some background processing, for example, writing or updating the data to disk.

MORE INFORMATION
When a screen saver starts, it posts a WM_SYSCOMMAND message to the foreground window with WPARAM as SC_SCREENSAVE. To detect and notify other applications of this event, use the following steps:

  Install WH_GETMESSAGE Global hook.  hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)HookProc,          (HINSTANCE) hMod, 0);   Define a user-defined registered message for example, "ScreenSaverStarted".   UINT WM_SCRNSVSTART = RegisterWindowMessage("ScreenSaverStarted");   Broadcast this message to all top-level windows in the system.   LRESULT CALLBACK   HookProc(UINT code, WPARAM wParam, LPARAM lParam) {   MSG *msg = (MSG *)lParam; if ( msg->message == WM_SYSCOMMAND && msg->wParam == SC_SCREENSAVE) {  // broadcast message to all top-level windows // Or execute some other code here PostMessage(HWND_BROADCAST, WM_SCRNSVSTART, 0, 0); }        // Always call next hook in chain return CallNextHookEx(hHook, code, wParam, lParam); }   Uninstall the hook.  UnhookWindowsHookEx(hHook); On Windows 98 and Windows 2000, you can query the system by calling SystemParametersInfo with uiAction as SPI_GETSCREENSAVERRUNNING.   BOOL bActive; SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, NULL,(LPVOID) &bActive, NULL); //bActive will be true if Screen Saver is running 