终止正在运行的屏幕保护程序
2013-10-14 09:23
567 查看
获取当前是否进入屏幕保护程序SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &isActive, 0);
Sometimes applications need to terminate a screen saver that is already running. In Microsoft 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);
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.
The process of obtaining a handle to the currently active screen saver window is different for operating systems that are later than Microsoft Windows 95. On Microsoft Windows NT, on Microsoft Windows 2000,
and on Microsoft Windows Server 2003, the screen saver may run on either the default desktop or on a separate desktop, depending on a setting in the display properties:
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.
You cannot use either the GetForegroundWindow() function or the
FindWindow function to determine whether the screen saver is currently active:
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.
Therefore, Microsoft recommends the following:
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.
Note that terminating a screen saver that is already running as demonstrated above is totally separate from disabling the screen saver altogether, so that no screen saver starts after the designated time period expires. This can be done easily using:
This method works well for terminating the currently running screen saver. However, one problem that you might encounter is that the system will not restart the screen saver unless the user moves the mouse or presses a key. If you need the screen saver to start
up again, you'll need to reinitialize the time-out period. Do this by using one of the following methods:
Calling SystemParametersInfo (SPI_SETSCREENSAVEACTIVE, TRUE, 0, SPIF_SENDWININICHANGE).
Using SetCursorPos() to simulate user input.
Both of these methods will cause the system to restart the time-out counter for the screen saver.
Article ID: 140723 - Last Review: February 22, 2007 - Revision: 3.2
APPLIES TO
Microsoft Win32 Software Development Kit (SDK) 3.1, when used with:
Microsoft Windows NT Advanced Server 3.1
Microsoft Windows NT 4.0
Microsoft Windows NT 3.51 Service Pack 5
Microsoft Windows NT 4.0
Microsoft Windows 2000 Standard Edition
Microsoft Windows Server 2003, Standard Edition (32-bit x86)
Sometimes applications need to terminate a screen saver that is already running. In Microsoft 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);
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.
MORE INFORMATION
The process of obtaining a handle to the currently active screen saver window is different for operating systems that are later than Microsoft Windows 95. On Microsoft Windows NT, on Microsoft Windows 2000,and on Microsoft Windows Server 2003, the screen saver may run on either the default desktop or on a separate desktop, depending on a setting in the display properties:
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.
You cannot use either the GetForegroundWindow() function or the
FindWindow function to determine whether the screen saver is currently active:
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.
Therefore, Microsoft recommends the following:
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) { 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); } else { // 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); }
Note that terminating a screen saver that is already running as demonstrated above is totally separate from disabling the screen saver altogether, so that no screen saver starts after the designated time period expires. This can be done easily using:
SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, FALSE, 0, SPIF_SENDWININICHANGE );
This method works well for terminating the currently running screen saver. However, one problem that you might encounter is that the system will not restart the screen saver unless the user moves the mouse or presses a key. If you need the screen saver to start
up again, you'll need to reinitialize the time-out period. Do this by using one of the following methods:
Calling SystemParametersInfo (SPI_SETSCREENSAVEACTIVE, TRUE, 0, SPIF_SENDWININICHANGE).
Using SetCursorPos() to simulate user input.
Both of these methods will cause the system to restart the time-out counter for the screen saver.
Properties
Article ID: 140723 - Last Review: February 22, 2007 - Revision: 3.2APPLIES TO
Microsoft Win32 Software Development Kit (SDK) 3.1, when used with:
Microsoft Windows NT Advanced Server 3.1
Microsoft Windows NT 4.0
Microsoft Windows NT 3.51 Service Pack 5
Microsoft Windows NT 4.0
Microsoft Windows 2000 Standard Edition
Microsoft Windows Server 2003, Standard Edition (32-bit x86)
Keywords: | kbcode kbhowto kbscreensaver KB140723 |
相关文章推荐
- 在root下运行gdesklets和屏幕保护程序
- 在ubuntu中,终止一个进程或终止一个正在运行的程序
- android 屏幕保护程序--------Fade In 与 Fade Out 特效与运行线程
- matlab 终止正在运行的程序
- 如何优雅地终止正在运行的Spark Streaming程序
- 基于JUnit和Ant测试程序正在运行使用Kieker(AspectJ)监测方法
- 第二章-Windows程序运行原理——(1) CPU保护模式和Windows系统
- c#简单屏幕保护程序,共4个线程
- 编写程序,当程序运行后,根据屏幕提示输入一个数字字符串,输入后统计有多少个偶数数字和奇数数字。
- 如何使PC上运行的程序不能退出,并且用户只能使用当前正在运行的程序。不能使用PC上的其他程序
- java程序中,如何安全的结束一个正在运行的线程
- Installshield 在安装或者卸载过程中,判断某一程序是否正在运行
- 一个简易的屏幕保护程序
- iOS开发中测试机或者模拟器运行时程序没有占整个屏幕问题
- [转]由于另一个程序或正在运行的服务可能正在使用网络地址转换组件(Ipnat.sys)...
- MAC上搭建Windows Phone 8 开发环境——VMware Fusion下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法
- vc-如何关闭正在运行的程序??
- 黑客帝国_屏幕保护程序
- stm32f4 RAM中运行程序 读保护设置 - tofixer
- 在4寸屏幕上运行程序,出现的是3.5寸屏幕