Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Screen Saver appears black....

Posted on 2000-04-12
10
Medium Priority
?
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 

Author Comment

by:Primistic
ID: 2707580
Adjusted points from 30 to 50
0
 

Expert Comment

by:rich_clarke
ID: 2707682
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
ID: 2707701
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Primistic
ID: 2710219
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 100 total points
ID: 2711919
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
 

Author Comment

by:Primistic
ID: 2722550
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
ID: 2722674
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
ID: 2730373
Although the problem is not yet solved.. and thanks to your great help..  the points for you..
0
 
LVL 22

Expert Comment

by:nietod
ID: 2730386
Thanks, althoguh it really is not necessary...

Did you get answers to any of those questions?
0
 

Author Comment

by:Primistic
ID: 2733768
nope
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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