Processing Screen Saver Messages

Your screen-saver module must include a ScreenSaverProc window function to receive and process messages for the screen-saver window. Declare the ScreenSaverProc function as you would any window function:

LONG FAR PASCAL ScreenSaverProc(HWND hWnd, WORD msg, WORD wParam, LONG lParam)

Your ScreenSaverProc function can process messages, but it must pass unprocessed messages to the DefScreenSaverProc function rather than to the DefWindowProc function.

Be sure to export the ScreenSaverProc function by including it in the EXPORTS section of your module definition (.DEF) file.

The following code fragment shows the ScreenSaverProc function for a screen saver that substitutes a gray screen for the default black screen:

LONG FAR PASCAL ScreenSaverProc(HWND hWnd, WORD wMsg, WORD wParam,
LONG lParam)
{
        RECT rc;

        switch(wMsg)
        {
                case WM_ERASEBKGND:
                        GetClientRect(hWnd,&rc);
                        FillRect(wParam,&rc,GetStockObject(GRAY_BRUSH));
                        return 0l;                              // Avoid default action
        }
        return DefScreenSaverProc (hWnd, wMsg, wParam, lParam);
}

Summary: Handling WM_CREATE and WM_DESTROY

The preceding screen saver does not perform any actions in response to the WM_CREATE and WM_DESTROY messages. However, you can use these messages as a cue to initialize variables and then clean up before termination.

If your window procedure traps the WM_DESTROY message, it must do one of the following to properly end the screen saver:

After processing the message, pass it to DefScreenSaverProc

In the WM_DESTROY case of the message handler, call PostQuitMessage to properly end the screen saver.

Your ScreenSaverProc function can substitute its own actions for the messages handled by DefScreenSaverProc. The DefScreenSaverProc function responds as follows to key window messages:

Message Response

WM_ACTIVATE WM_ACTIVATEAPP If wParam is FALSE, terminates the screen saver. A wParam value of FALSE indicates that the screen saver is losing focus.
WM_ERASEBKGND Paints the screen background black.
WM_SETCURSOR Removes the cursor from the screen by setting the cursor to NULL.
WM_KEYDOWN WM_MOUSEMOVE WM_LBUTTONDOWN WM_MBUTTONDOWN WM_RBUTTONDOWN Posts a WM_CLOSE message to close the screen-saver window.
WM_DESTROY Calls PostQuitMessage to terminate the screen saver.