HOWTO: Force a Screen Saver to Close Once Started in Windows NTID: Q140723
|
Sometimes applications need to terminate a screen saver that is already
running. In Windows 3.1 or Windows 95, a screen saver could be terminated
easily by posting a WM_CLOSE message to the currently active screen saver
window as in this example:
PostMessage (GetActiveWindow(), WM_CLOSE, 0, 0L);
Obtaining a handle to the currently active screen saver window under Windows
NT is not as straightforward as it is in Windows 3.1 and Windows 95.
Calling GetForegroundWindow() returns NULL because the screen saver is
running on a different desktop than the calling application. Similarly,
calling FindWindow ("WindowsScreenSaverClass", NULL) to determine if the
screen saver is currently active does not work either.
To do this, get a handle to the screen saver's desktop, enumerate that
desktop's windows, and then post a WM_CLOSE to the screen saver window.
The following code demonstrates how to do this. Note that if a screen saver
password is set, the following code brings up the password dialog box,
prompts the user for a password, and then actually terminates the screen
saver application.
BOOL CALLBACK KillScreenSaverFunc(HWND hwnd, LPARAM lParam)
{
if(IsWindowVisible(hwnd))
PostMessage(hwnd, WM_CLOSE, 0, 0);
return TRUE;
}
HDESK hdesk;
hdesk = OpenDesktop(TEXT("Screen-saver"),
0,
FALSE,
DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
if (hdesk)
{
EnumDesktopWindows(hdesk, KillScreenSaverFunc, 0);
CloseDesktop(hdesk);
}
SystemParametersInfo( SPI_SETSCREENSAVEACTIVE,
FALSE,
0,
SPIF_SENDWININICHANGE
);
Additional query words: deactivate disable stop running turn off screensaver
Keywords : kbcode kbNTOS kbScreenSaver kbGrpUser
Version : WINDOWS:
Platform : WINDOWS
Issue type :
Last Reviewed: April 29, 1999