Skip navigation

Preventing or stopping the screensaver from going on from your C++ Windows program

To intercept the screensaver on Win32, you trap the WM_SYSCOMMAND message in your WndProc, then you check the value of wparam. SC_SCREENSAVE is the screensaver and SC_MONITORPOWER is that windowsy force that wants to shut off the monitor to save power.

To disable either, just return 0 from the event handler function under those cases.

Example:

// This is a piece of WNDPROC:
LRESULT CALLBACK WndProc(   HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam ) 
{
    switch( message )
    {
    .
    .
    .
        case WM_SYSCOMMAND:
        {
            // There is a system command
            // wparam has the exact command type that it is
            switch( wparam )
            {
                case SC_MOVE:
                    printf("You're moving the window!\n");
                    // don't interfere with this one, otherwise your window
                    // won't move normally when the user tries to move it!
                    break;

                case SC_SCREENSAVE:     // screensaver wants to begin
                    
                    return 0;           // returning 0 PREVENTS those things from happening
                    // Note about SC_SCREENSAVE:
                    // Try this.  Go into your settings and change
                    // your screensaver to start after 1 minute.
                    // Then run this program and sit and wait
                    // for the 1 minute.

                    // The funny thing about this, is Windows will
                    // keep trying to enter screen saver mode,
                    // by sending your app a "message" every
                    // half a second or so.

                    // If all you do is return 0; from this
                    // part, your app will keep on stopping
                    // the screensaver from starting.  And
                    // Windows will keep asking if it can
                    // start the screen saver or not, until
                    // the user does something to reset the
                    // screensaver-turn-on timer like move the mouse
                    // or press a key on the keyboard.

                    // if you wait even longer, the same thing
                    // happens with SC_MONITORPOWER, except
                    // you keep getting 2 messages now.
                    
                    case SC_MONITORPOWER:   // monitor wants to shut off - powersaver mode
                        return 0;           // returning 0 PREVENTS monitor from turning off
            } // end wparam inner switch
        } //end case WM_SYSCOMMAND
            .
            .
            .
    } // end switch(message)
} // end WndProc
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: