Solved

Screen Saver appears black....

Posted on 2000-04-12
10
369 Views
Last Modified: 2008-02-26
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
Comment
Question by:Primistic
  • 5
  • 4
10 Comments
 

Author Comment

by:Primistic
Comment Utility
Adjusted points from 30 to 50
0
 

Expert Comment

by:rich_clarke
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:Primistic
Comment Utility
To:nietod

I still don't quite understand about the memory DC you are talking about.. you mind show me some codings???
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Primistic
Comment Utility
but still cannot.. don't know why??   beside the delete object... is there anymore wrong with my code???
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Are you sure the code is being executed?
can you do any sort of drawing, like a FillRect() from the code?
0
 

Author Comment

by:Primistic
Comment Utility
Although the problem is not yet solved.. and thanks to your great help..  the points for you..
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Thanks, althoguh it really is not necessary...

Did you get answers to any of those questions?
0
 

Author Comment

by:Primistic
Comment Utility
nope
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now