Primistic
asked on
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(HAND LE);
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(HAND LE 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);
}
Below is the code...
#include <windows.h>
#include <scrnsave.h>
#include "scrsav.h"
LRESULT WINAPI ScreenSaverProc(HWND, UINT, WPARAM, LPARAM);
BOOL WINAPI ScreenSaverConfigureDialog
BOOL WINAPI RegisterDialogClasses(HAND
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(HAND
{
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
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);
}
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.
Also look at the sample programs which come with your C++ compiler. One of them will show how to display a bitmap.
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.)?
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.)?
ASKER
To:nietod
I still don't quite understand about the memory DC you are talking about.. you mind show me some codings???
I still don't quite understand about the memory DC you are talking about.. you mind show me some codings???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
can you do any sort of drawing, like a FillRect() from the code?
ASKER
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?
Did you get answers to any of those questions?
ASKER
nope
ASKER