Home Articles Books Downloads FAQs Tips

Q: Prevent the screensaver from running


Answer

You can use two techniques to prevent the screensaver from starting. You can respond to WM_SYSCOMMAND messages, or you can call the SystemParametersInfo API function. This FAQ describes both techniques.



SystemParametersInfo:

SystemParametersInfo allows you to get and set a host of system settings. Among other things, it allows you to turn off the screensaver. You can also get and set the screensaver delay time. The code segment below shows a program with two button click handlers. One button deactivates the screensaver, the other turns it on. The constructor of the form determines if the screensaver was originally active when the program started , and what the screensaver delay was set to. As an added bonus, I threw in a function that sets the screensaver timeout to 30 seconds.

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    // Find out if the screensaver was active and what the
    // screen saver timeout value is.
    BOOL bActive;
    int  nTimeout;
    SystemParametersInfo(SPI_GETSCREENSAVEACTIVE,  0, &bActive, 0);
    SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &nTimeout, 0);

    if(bActive)
        Label1->Caption = "Screensaver was active";
    else
        Label1->Caption = "Screensaver was turned off";

    Label2->Caption = "Screensaver delay time = " + IntToStr(nTimeout);
}
//---------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    // Enable the screen saver.
    SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
}
//---------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    // Disable the screen saver.
    SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE,NULL, 0);
}
//---------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    // set the screensaver timeout to 30 seconds.
    SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 30, NULL, 0);
}


WM_SYSCOMMAND

Before Windows starts the screensaver, it broadcasts a WM_SYSCOMMAND message with WPARAM set to SC_SCREENSAVE. If any program catches the message and returns true, the screensaver will not start. Here is a code example that shows how to prevent the screensaver from running by detecting the WM_SYSCOMMAND message.

//----------------------------------------------
// class header file
private:	    // User declarations
    void __fastcall WndProc(Messages::TMessage &Message);


//----------------------------------------------
// CPP source file
void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
    if( (Message.Msg    == WM_SYSCOMMAND) &&
        (Message.WParam == SC_SCREENSAVE)    )
    {
        ShowMessage("screensaver disabled");
        Message.Result = true;
    }
    else
        TForm::WndProc(Message);
}


Note: If you deactivate the screensaver with SytemParametersInfo, make sure that you reenable it before your program terminates. The screensaver remains disabled until somebody activates it again.

Note: SytemParametersInfo enables the screensaver, but it doesn't actually execute it.

Note: Enabling the screensaver with SytemParametersInfo does not guarantee that the screensaver will run. If the user has turned of the screensaver via control panel, the OS will not activate the screensaver.

Note: Intercepting the WM_SYSCOMMAND message is probably more robust than using SytemParametersInfo because another program can call SystemParametersInfo and override any previous settings that you make.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.