Screen Saver Enabler/Disabler

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Legend: func = function; prog = program; ptr = pointer; SS
= Screen Saver; var = variable; Win = Windows;

This code enables / disables
the current active screen saver through a call to the SytemParametersInfo
function. This code also demonstrates pointer usage in CheckSSStatus()
for programmers who are learning pointers or who need a review.

Check out the Onbuttonenable(), Onbuttondisable() & CheckSSStatus()
funcs. This code should be fairly straightforward.

This is only my second Win32 prog, also only my second prog
in C++/VC++. When I last programmed, I was using Pascal on a 386
SX-16 w/ MS-DOS 5.0 & Win 3.1!

You may use this code in any manner you wish, but I request
an email notifying me of the use of the code and a return email
address so I can reply to you.

Please send any improvements, bug reports, complaints or modifications
you may have…as I am a beginner. My name is Marc and my email
is: mehowe@yahoo.com.
You will be credited if I use the modifications in any program
I write.

This program was written because I burn CD-R’s and the SS should
be disabled while doing so to avoid transfer interruptions. I
didn’t want to have to go to my desktop and right-click to open
the display properties or open the Control Panel from the start
menu. Really, I just wanted to see if I could write a little utility
to disable or enable a SS.

I did not find an article already on CodeGuru dealing with
this issue, so here is the code.

This code should work under UNICODE as well. I have only tested
it on two separate machines running Win ’98. According to MSDN
documentation, this code will work on NT 4.0. I am still considering
switching to NT 4.0…maybe.

This code was written in the MSVC++ 6.0 Pro Edition (w/SR-2
update).

Overview of code:

NOTE: In SSED’s project settings, I used MFC as a Statically Linked
Library. This causes the compiler to give the following two warnings
when set to the Level 4 error checking state:

LINK : warning LNK4089: all references to "SHELL32.dll"
discarded by /OPT:REF

LINK : warning LNK4089: all references to "comdlg32.dll"
discarded by /OPT:REF

If you receive either of these warnings, you can ignore them
(according to documentation). This is just letting you know that
the MFC is being used as a Statically Linked Library.

Now, the real stuff. First, the InitDialog() func calls the
CheckSSStatus() func to determine the current SS Status and update
the dialog’s edit box with the SS state’s value.

Next, if the Enable button is pressed, it calls the Onbuttonenable()
func. The Onbuttonenable() func creates a local boolean ptr var
named *pvParam…this is the name given to the parameter in the
MSDN SystemParametersInfo() documentation. Next, the Win SystemParametersInfo()
func is called with the SPI_GETSCREENSAVEACTIVE parameter and
is passed a reference to pvParam as &pvParam. This func is
called to get the SS current state. Next, the if construct tests
pvParam to see if it is set to TRUE, if it is, then the function
does nothing and exits. If pvParam is set to FALSE, then it calls
the Win SystemParametersInfo() func passing it the SPI_SETSCREENSAVEACTIVE
parameter and setting it to TRUE…meaning that the last SS the
user used is now enabled. Lastly, a call to the CheckSSStatus()
func is made to test whether the call to the SPI_SETSCREENSAVEACTIVE()
func performed properly.

The Disable button calls the Onbuttondisable() func and works
in the same manner as the Onbuttonenable() func, except sending
the FALSE value to disable the current SS.

The CheckSSStatus() func initializes in basically the same
manner as the above functions. However, it also sets a CString
member var named m_strEditSSStatus to "ENABLED" or "DISABLED"
depending on the SS current status and then sends it the the dialog’s
edit box. It then updates the dialog’s display to show the user
the current SS state.

void CSSEDDlg::OnButtonenable()
{
// TODO: Add your control notification handler code here
bool *pvParam; //declare local bool ptr variable for the SystemParametersInfo func()

SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &pvParam, 0); //Get SS state
if (pvParam != FALSE)
{
// If SS already enabled, do nothing
} //end if
// If SS enabled, disable it
else if (pvParam == FALSE)
{
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, 0, 0);
//Enable screen saving
//SPIF_SENDWININICHANGE (SystemParametersInfo Parameter #4 alternative, see MSDN documentation)

//Call CheckSSStatus() func to check performance of SystemParameterInfo() func
//& update the dialog’s display with the SS status

CheckSSStatus();
} //end else if
} //End OnButtonEnable()

/Onbuttondisable() func is same, except FALSE is set…see SSEDdlg.cpp for details.

void CSSEDDlg::CheckSSStatus(void)
{
bool *pvParam; //declare local bool ptr variable for the SystemParametersInfo func()

SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &pvParam, 0);
if (pvParam == FALSE)
{
// If SS disabled, send "DISABLED" to read-only edit control and update the display
m_strEditSSStatus = "DISABLED"; //m_EditSSStatus is a member Cstring var for the dialog’s edit box
UpdateData(FALSE);
} //end if
else if (pvParam != FALSE)
{
// If SS enabled, send "ENABLED" to read-only edit control and update the display
m_strEditSSStatus = "ENABLED"; //m_EditSSStatus is a member Cstring var for the dialog’s edit box
UpdateData(FALSE);
} //end if
} //end CheckSSStatus()

LAST NOTES:

LIMITATIONS:

1. (Intentional) Once a button is pushed, the SS enable/diable
action is carried out…no way to cancel. This prog was meant
to be simple and small. The user must use the other button to
reset the SS or go to the display properties via desktop or Control
Panel to change it back.

2. (Intentional) No OK or CANCEL buttons as ESC key or the [X]
button on the system title bar will close the prog. Program does
not eat the ESC key intentionally.

SYSTEM REQUIREMENTS: Appears to use about 200K RAM when running.
May run on any Win’9x or Win NT system…see "Tested On"
below.

TESTED ON: Two Pentium II machines running Win’98. These machines
are also running the freeware CPU cooler utility "RAIN"–highly
recommended for anyone using Win’9x to keep their CPU cool. Win
NT 4.0 already has a built-in CPU cooling routine as part of its
OS. Neither system has experienced any conflicts with RAIN, ever!
I am in no way affiliated with the RAIN programmers, I just recommend
it for anyone using a Win ‘9x Pentium II (or better) machine as
these CPU’s get incredibly HOT!

LEGAL/WARRANTY: This software is (C) Copyrighted 1999 by Marc
E. Howe, All Rights Reserved. The author makes no warranties,
either expressed or implied regarding the use of any of the SSED
software. The user is wholly responsible for any and all damages
resulting from the use of this software. The author is fully free
from all liability regarding the use of this software by the consumer.
You may use this code in any manner you wish, but the author requests
an email notifying him of the use of the code and a return email
address so the author can reply. By using this software, you agree
to all of the terms in the above legal / warranty statements.

Download demo project – 97 KB

Date Last Updated: April 24, 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read