[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

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"

LRESULT WINAPI ScreenSaverProc(HWND, UINT, WPARAM, LPARAM);
BOOL WINAPI ScreenSaverConfigureDialog(HWND, UINT, WPARAM, LPARAM);
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
                  vLoadStrings();

                  // 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);

                  break;

            case WM_TIMER:
                  MoveImage(hWnd);
                  break;

            case WM_DESTROY:
                  // when finished, delete bitmaps
                  if(hBitmap1)
                        DeleteObject(hBitmap1);
                  if(hBitmap2)
                        DeleteObject(hBitmap2);
                  if(hBitmap3)
                        DeleteObject(hBitmap3);
                  if(hBitmap4)
                        DeleteObject(hBitmap4);

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

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

            default:
                  break;
      }
      return (DefScreenSaverProc(hWnd, messg, wParam, lParam));
}
// Register the window
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
      return true;
}
void vLoadStrings(VOID)
{
      LoadString(hMainInstance, idsAppName, szAppName,
                        APPNAMEBUFFERLEN);
      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);
      Sleep(iDelay);

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

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

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

      // 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 < rcWnd.top))
            yStep =- yStep;      

      // do some erasing on the screen
      rcFill.left = xPos;
      rcFill.right = xPos + bm.bmWidth;
      rcFill.bottom = yPos;
      rcFill.top = yPos + bm.bmHeight;
      FillRect(hdc, &rcFill, (HBRUSH) GetStockObject(BLACK_BRUSH));
      DeleteDC(hmdc);
      ReleaseDC(hWnd, hdc);
}
0
Primistic
Asked:
Primistic
  • 5
  • 4
1 Solution
 
PrimisticAuthor Commented:
Adjusted points from 30 to 50
0
 
rich_clarkeCommented:
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.

0
 
nietodCommented:
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.)?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PrimisticAuthor Commented:
To:nietod

I still don't quite understand about the memory DC you are talking about.. you mind show me some codings???
0
 
nietodCommented:
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);

     *   *   *

SelectObject(hmdc,OriginalBitmap);
DeleteDC(hmdc);
0
 
PrimisticAuthor Commented:
but still cannot.. don't know why??   beside the delete object... is there anymore wrong with my code???
0
 
nietodCommented:
Are you sure the code is being executed?
can you do any sort of drawing, like a FillRect() from the code?
0
 
PrimisticAuthor Commented:
Although the problem is not yet solved.. and thanks to your great help..  the points for you..
0
 
nietodCommented:
Thanks, althoguh it really is not necessary...

Did you get answers to any of those questions?
0
 
PrimisticAuthor Commented:
nope
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now