Microsoft Windows NT, however, introduces the concept of separate desktops, so that applications can run on one, and screen savers can run on another. This makes screen saver termination under Windows NT, Windows 2000, and Windows Server 2003 a bit more difficult.
- If you select the On resume, password protect check box on the Screen Saver tab of the Display Properties dialog box, the screen saver runs on its own desktop.
- If you clear the check box that is described in the previous point, the screen saver runs on the default desktop.
- The GetForegroundWindow() function returns NULL because the screen saver is running on a different desktop from the desktop that the calling application is running on.
- The FindWindow function ("WindowsScreenSaverClass", NULL) does not work either.
- Code to dismiss the screen saver first determines whether the screen saver is running on its own desktop.
- If the screen saver is running on its own desktop, the code closes the screen saver there.
- If there is no desktop for the screen saver, the code uses the default desktop.
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)
PostMessage(hwnd, WM_CLOSE, 0, 0);
hdesk = OpenDesktop(TEXT("Screen-saver"), 0, FALSE, DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
EnumDesktopWindows(hdesk, KillScreenSaverFunc, 0);
// Windows 2000 and later:
// If there is no screen saver desktop, the screen saver
// is on the default desktop. Close it by sending a
// WM_CLOSE. PostMessage(GetForegroundWindow(), WM_CLOSE, 0, 0L);
- Calling SystemParametersInfo (SPI_SETSCREENSAVEACTIVE, TRUE, 0, SPIF_SENDWININICHANGE).
- Using SetCursorPos() to simulate user input.
Article ID: 140723 - Last Review: Jun 22, 2014 - Revision: 1