控制屏幕保护程序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