控制螢幕保護裝置程式API函數的C++類封裝
by 郭世龍
簡 介 前幾天在codeproject上看到了一篇關於用C#封裝控制的螢幕保護裝置程式的文章,覺的很有用於是決定將其改裝成C++類封裝以供C++程式員使用。這個類提供了查詢螢幕保護裝置程式資訊的函數,包括是否啟用、是否正在運行、查詢和設定屏保等待時間、強制關閉螢幕保護裝置程式等函數。user32.dll 動態連結程式庫提供的SystemParametersInfo()API函數(需要包含標頭檔Windows.h)提供了對螢幕保護裝置程式控制的功能。
函數介紹
bool GetScreenSaverActive( ) —— 判斷螢幕保護裝置程式是否開啟,如果開啟則返回true否則返回false
void SetScreenSaverActive(int Active) —— 傳遞參數1來開啟螢幕保護裝置程式, 傳遞0來關閉螢幕保護裝置程式
int GetScreenSaverTimeout( ) —— 返回當前螢幕保護裝置程式等待時間設定,以秒位單位
void SetScreenSaverTimeout(int Value) —— 設定螢幕保護裝置程式等待時間,以秒位單位
bool GetScreenSaverRunning( ) —— 判斷當前螢幕保護裝置程式是否正在運行,是則返回ture否則返回false
void KillScreenSaver( ) —— 強制結束螢幕保護裝置
封裝代碼 使用時,拷貝代碼到程式中,引入這個ScreenSaverCtrl類檔案即可。 標頭檔://ScreenSaverCtrl.h#pragma once#include "Windows.h"/* //靜態連結
#pragma comment(lib,"User32.lib") //在link時,連結到User32.lib檔案extern "C"
{
WINUSERAPI BOOL WINAPI SystemParametersInfoW(__in UINT uiAction,__in UINT uiParam,__inout_opt PVOID pvParam,__in UINT fWinIni);
WINUSERAPI BOOL WINAPI SystemParametersInfoA(__in UINT uiAction,__in UINT uiParam,__inout_opt PVOID pvParam,__in UINT fWinIni);
WINUSERAPI BOOL WINAPI PostMessageA(__in_opt HWND hWnd,__in UINT Msg, __in WPARAM wParam,__in LPARAM lParam);
WINUSERAPI HDESK WINAPI OpenDesktopA(__in LPCSTR lpszDesktop,__in DWORD dwFlags,__in BOOL fInherit,__in ACCESS_MASK dwDesiredAccess);
WINUSERAPI BOOL WINAPI CloseDesktop(__in HDESK hDesktop);
WINUSERAPI BOOL WINAPI EnumDesktopWindows( __in_opt HDESK hDesktop, __in WNDENUMPROC lpfn, __in LPARAM lParam);
WINUSERAPI BOOL WINAPI IsWindowVisible(__in HWND hWnd);
WINUSERAPI HWND WINAPI GetForegroundWindow(VOID);
WINUSERAPI BOOL WINAPI EnumDesktopWindows(__in_opt HDESK hDesktop,__in WNDENUMPROC lpfn,__in LPARAM lParam); }
*/
//BOOL CALLBACK KillScreenSaverFunc(HWND hWnd,LPARAM lParam);class ScreenSaverCtrl
{
public:
ScreenSaverCtrl(void);
static bool GetScreenSaverActive();
static void SetScreenSaverActive(int Active);
static int GetScreenSaverTimeout();
static void SetScreenSaverTimeout(int Value);
static bool GetScreenSaverRunning();
static void KillScreenSaver(); private:
static BOOL CALLBACK KillScreenSaverFunc(HWND hWnd,LPARAM lParam);
public:
~ScreenSaverCtrl(void);
};
實現檔案: //ScreenSaverCtrl.cpp#include "ScreenSaverCtrl.h" /*
BOOL CALLBACK KillScreenSaverFunc(HWND hWnd,LPARAM lParam) //注意回呼函數不能為普通成員函數,但可為靜態成員函數
{
if( IsWindowVisible(hWnd) ) PostMessage( hWnd, WM_CLOSE, 0, 0 );
return true;
}
*/ ScreenSaverCtrl::ScreenSaverCtrl(void{
} bool ScreenSaverCtrl::GetScreenSaverActive( )
{
BOOL isActive = FALSE; //一定是BOOL而不是bool否則執行階段錯誤 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE,0,&isActive, 0);
return isActive;
} void ScreenSaverCtrl::SetScreenSaverActive( int Active )
{
int nullVar = 0; SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,Active, &nullVar, SPIF_SENDWININICHANGE );
} int ScreenSaverCtrl::GetScreenSaverTimeout( )
{
int value = 0; SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,&value, 0 );
return value;
} void ScreenSaverCtrl::SetScreenSaverTimeout(int Value )
{
int nullVar = 0; SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,Value, &nullVar,SPIF_SENDWININICHANGE );
} bool ScreenSaverCtrl::GetScreenSaverRunning( )
{
BOOL isRunning = FALSE; SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0,&isRunning, 0 );
return isRunning;
} void ScreenSaverCtrl:: KillScreenSaver( )
{
HDESK hDesktop = OpenDesktop(TEXT("Screen-saver"), 0,false,DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
if( hDesktop != NULL)
{
EnumDesktopWindows(hDesktop,KillScreenSaverFunc,0);
CloseDesktop( hDesktop );
}
else
{
PostMessage( GetForegroundWindow( ), WM_CLOSE, 0, 0 );
}
} //注意回呼函數不能為非靜態成員函數,因為其有隱含參數this 而靜態成員函數沒有this指標// 關於回呼函數參見文章:http://blog.csdn.net/xylary/archive/2007/04/01/1548596.aspx
BOOL ScreenSaverCtrl::KillScreenSaverFunc(HWND hWnd,LPARAM lParam) {
if( IsWindowVisible(hWnd) ) PostMessage( hWnd, WM_CLOSE, 0, 0 );
return true;
} ScreenSaverCtrl::~ScreenSaverCtrl(void)
{}
測試程式 編譯運行環境 作業系統:Windows XP SP2 編譯器:VS2005 測試程式介面 測試程式下載Orignal Refrence Text