Screen Saver appears black....

Why my screen saver appears black??  I try not to set the coords out of range... but I still can't get it right?? WHY?????

Below is the code...
#include <windows.h>
#include <scrnsave.h>
#include "scrsav.h"

BOOL WINAPI RegisterDialogClasses(HANDLE);
void vLoadStrings(VOID);
void MoveImage(HWND hWnd);

char szBMName1[]="BMImage1";
char szBMName2[]="BMImage2";
char szBMName3[]="BMImage3";
char szBMName4[]="BMImage4";
HBITMAP hBitmap1, hBitmap2;  // bitmap handles
HBITMAP hBitmap3, hBitmap4;

static int xPos;            // X position
static int yPos;            // Y position
static int xPosInit;      // init X position
static int yPosInit;      // init Y position
static int xStep;            // X step size
static int yStep;            // Y step size
static int iDelay;            // image delay
int iTimer;            // speed control for image

// main entry point for screen saver messages.
LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT messg,
                                             WPARAM wParam, LPARAM lParam){
      RECT rc;
      switch (messg)
            case WM_CREATE:
                  // load screen saver strings

                  // get initial settings for image
                  xPosInit = 30;
                  yPosInit = 30;
                  xStep = 2;
                  yStep = 2;
                  xPos = xPosInit;
                  yPos = yPosInit;
                  iDelay = 40;

                  // load all four bitmapped images
                  hBitmap1 = LoadBitmap(hMainInstance, szBMName1);
                  hBitmap2 = LoadBitmap(hMainInstance, szBMName2);
                  hBitmap3 = LoadBitmap(hMainInstance, szBMName3);
                  hBitmap4 = LoadBitmap(hMainInstance, szBMName4);

                  // start system timer
                  iTimer = SetTimer(hWnd, ID_TIMER, iDelay, NULL);


            case WM_TIMER:

            case WM_DESTROY:
                  // when finished, delete bitmaps

                  // destroy the system timer
                  KillTimer(hWnd, ID_TIMER);
                  sndPlaySound(NULL, 0);

            case WM_ERASEBKGND:
                  // prepare for erasing whole screen
                  GetClientRect(hWnd, &rc);
                  FillRect((HDC)wParam, &rc, (HBRUSH) GetStockObject(BLACK_BRUSH));
                  return 0L;

      return (DefScreenSaverProc(hWnd, messg, wParam, lParam));
// Register the window
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
      return true;
void vLoadStrings(VOID)
      LoadString(hMainInstance, idsAppName, szAppName,
      LoadString(hMainInstance, IDS_NAME, szName, TITLEBARNAMELEN);
      LoadString(hMainInstance, idsScreenSaver, szScreenSaver, 22);
      LoadString(hMainInstance, IDS_DESCRIPTION, "TesT", 4);
// Screen Saver interface
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg,
                                                         UINT messg,
                                                         WPARAM wParam,
                                                         LPARAM lParam)
      return FALSE;
void MoveImage(HWND hWnd)
      HDC hdc, hmdc;
      BITMAP bm;
      RECT rcWnd;
      static RECT rcFill;

      GetClientRect(hWnd, &rcWnd);
      GetObject(hBitmap1, sizeof(bm), (LPSTR)&bm);

      xPos += xStep;
      yPos += yStep;

      hdc = GetDC(hWnd);
      hmdc = CreateCompatibleDC(hdc);

      // draw image 1
      SelectObject(hmdc, hBitmap1);
      GetObject(hBitmap1, sizeof(bm), (LPSTR)&bm);
      BitBlt(hdc, xPos, yPos, bm.bmWidth, bm.bmHeight,
                  hmdc, 0, 0, SRCCOPY);

      // draw image 2
      SelectObject(hmdc, hBitmap1);
      GetObject(hBitmap2, sizeof(bm), (LPSTR)&bm);
      BitBlt(hdc, xPos, yPos, bm.bmWidth, bm.bmHeight,
                  hmdc, 0, 0, SRCCOPY);

      // draw image 3
      SelectObject(hmdc, hBitmap1);
      GetObject(hBitmap3, sizeof(bm), (LPSTR)&bm);
      BitBlt(hdc, xPos, yPos, bm.bmWidth, bm.bmHeight,
                  hmdc, 0, 0, SRCCOPY);

      // draw image 4
      SelectObject(hmdc, hBitmap1);
      GetObject(hBitmap4, sizeof(bm), (LPSTR)&bm);
      BitBlt(hdc, xPos, yPos, bm.bmWidth, bm.bmHeight,
                  hmdc, 0, 0, SRCCOPY);

      // examine left and right window edges
      if((xPos + bm.bmWidth > rcWnd.right) || (xPos < rcWnd.left))
            xStep =- xStep;      

      // examine top and bottom window edges
      if((yPos + bm.bmHeight > rcWnd.bottom) || (yPos <
            yStep =- yStep;      

      // do some erasing on the screen
      rcFill.left = xPos;
      rcFill.right = xPos + bm.bmWidth;
      rcFill.bottom = yPos; = yPos + bm.bmHeight;
      FillRect(hdc, &rcFill, (HBRUSH) GetStockObject(BLACK_BRUSH));
      ReleaseDC(hWnd, hdc);
Who is Participating?

Improve company productivity with a Business Account.Sign Up

nietodConnect With a Mentor Commented:
Whenever you do a Selectobject() for the first time in a DC you MUST save the value that is returned.    You then MUST select this value back into the DC before you delete/release the DC.  The value that is returned in the "original" object of the type you are selecting in.  I.e when you first select in a brush, the return value is the brush that the DC originally had.  That brush must be returned to the DC before you delete or release the DC.  if you don't there will be several errors.  first of all, you will delete your brush, while it is still selected into a DC.  That is always an error.  Never delete any object (brush, bitmap, pen....) while it is selected into a DC.  Second, the original brush will never be deleted.  You didn't save the handle so you can't delete it, and you didn't return it to the OS so the OS can't delete it.  So this is an error.  (maybe, if it was a stock object it doesn't have to get deleted.)  Finally the object the in the DC when it is returned to the OS will not be the object the DC originally had.  This may cause problems the next time the DC is used.  (There are 4 general DCs shared by all programs, so this can have weird consequences.)

HBITMAP OriginaBitmap = (HBITMAP) SelectObject(hmdc, hBitmap1);
GetObject(hBitmap1, sizeof(bm), (LPSTR)&bm);

     *   *   *

PrimisticAuthor Commented:
Adjusted points from 30 to 50
Get a copy of "Programming Windows" by Charles Petzold. It explains how to use the Windows API.

Also look at the sample programs which come with your C++ compiler. One of them will show how to display a bitmap.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

You select a bitmap into a memory DC but never save the original bitmap.  so you can never select the original bitmap back in. So when you delete the  memory DC the original bitmap does not get deleted.  (plust other problems as a consequence).

You probably should try debugging some.  It woudl be helpful to narrow down the problem.

Do the bitmap's get loaded or are their handles set to NULL?
Are the coordinates valid?
Does the MoveImage() procedure ever get called?
Can you draw to the window DC using other means (Fill it with RED, for example.)?
PrimisticAuthor Commented:

I still don't quite understand about the memory DC you are talking about.. you mind show me some codings???
PrimisticAuthor Commented:
but still cannot.. don't know why??   beside the delete object... is there anymore wrong with my code???
Are you sure the code is being executed?
can you do any sort of drawing, like a FillRect() from the code?
PrimisticAuthor Commented:
Although the problem is not yet solved.. and thanks to your great help..  the points for you..
Thanks, althoguh it really is not necessary...

Did you get answers to any of those questions?
PrimisticAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.