Controls the C ++ class encapsulation of the Screen Saver API functions

Source: Internet
Author: User
Controls the C ++ class encapsulation of the Screen Saver API functions By Guo shilong IntroductionA few days ago, I saw an article on screen saver controlled by C # encapsulation in codeproject, therefore, I decided to convert it into a C ++ class package for C ++ programmers. This class provides functions for querying Screen Saver information, including enabling, running, querying, and setting Screen Saver wait times, and forcibly disabling screen saver. The systemparametersinfo () API function provided by the dynamic link library user32.dll (which must contain the header file windows. h) provides Screen Saver control functions. Function Introduction
  • bool GetScreenSaverActive( )-- Determines whether the Screen Saver is enabled. If enabled, true is returned. Otherwise, false is returned.
  • void SetScreenSaverActive(int Active)-- Pass parameter 1 to enable the screen saver, and pass 0 to disable the screen saver.
  • int GetScreenSaverTimeout( )-- Returns the waiting time of the current screen saver, in seconds.
  • void SetScreenSaverTimeout(int Value)-- Sets the wait time of the screen saver, in seconds.
  • bool GetScreenSaverRunning( )          -- Determines whether the current Screen Saver is running. If yes, true is returned. Otherwise, false is returned.
  • void KillScreenSaver( )-- Force end screen protection
Encapsulation codeDuring use, copy the code to the program and introduce the screensaverctrl file. Header file: // screensaverctrl. h # pragma once # include "windows. H"/* // static Link
# Pragma comment (Lib, "user32.lib") // link to the user32.lib file extern "C" during Link"
{
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 ,__ in uint MSG, _ in wparam ,__ in 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 );
Winuserapi bool winapi iswindowvisible (_ in hwnd );
Winuserapi hwnd winapi getforegroundwindow (void );
Winuserapi bool winapi enumdesktopwindows (_ in_opt hdesk hdesktop ,__ in wndenumproc lpfn ,__ in lparam );}
*/
// Bool callback killscreensaverfunc (hwnd, 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, lparam );

Public:
~ Screensaverctrl (void );
};
Implementation file: // screensaverctrl. cpp # include "screensaverctrl. H "/*
Bool callback killscreensaverfunc (hwnd, lparam) // note that the callback function cannot be a common member function, but can be a static member function.
{
If (iswindowvisible (hwnd) postmessage (hwnd, wm_close, 0, 0 );
Return true;
}
*/Screensaverctrl: screensaverctrl (void {

} Bool screensaverctrl: getscreensaveractive ()
{
Bool isactive = false; // It Must Be bool rather than bool; otherwise, the runtime error 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, shorttop_readobjects | shorttop_writeobjects );
If (hdesktop! = NULL)
{
Enumdesktopwindows (hdesktop, killscreensaverfunc, 0 );
Closedesktop (hdesktop );
}
Else
{
Postmessage (getforegroundwindow (), wm_close, 0, 0 );
}
} // Note that the callback function cannot be a non-static member function, because it has an implicit parameter this while the static member function does not have this pointer // for callback functions see the article: http://blog.csdn.net/xylary/archive/2007/04/01/1548596.aspx
Bool screensaverctrl: killscreensaverfunc (hwnd, lparam ){
If (iswindowvisible (hwnd) postmessage (hwnd, wm_close, 0, 0 );
Return true;
} Screensaverctrl ::~ Screensaverctrl (void)
{} Test procedureCompile and run the operating system: Windows XP SP2 Compiler: vs2005 Test Program Interface Test program download orignal refrence text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.