Sign in to follow this  
zebdor

Problems with bitmaps

Recommended Posts

zebdor    122
I have already posted a similar message in the beginners forum but I wasn't sure if this question belongs in there. I am having trouble loading a bitmap picture for a screensaver program I am making. I have already changed the program from .exe to .scr. Here is the code:
#include "stdafx.h"
#include "windows.h"
#include "resource.h"
#include "scrnsave.h"
#include "commctrl.h"

#pragma comment(lib, "scrnsave.lib")
#pragma comment(lib, "comctl32.lib")

HINSTANCE hInst=NULL;
HBITMAP pic=NULL;

LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message) 
	{
	case WM_CREATE:
		pic=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1));
		if(!pic)
		{
			MessageBox(hWnd,"Cannot start screen saver!","Uh-oh!",MB_OK|MB_ICONEXCLAMATION);
			return -1;
		}
		break;
	case WM_PAINT:
		BITMAP bm;
		HDC memDC;
		hdc=BeginPaint(hWnd, &ps);
		memDC=CreateCompatibleDC(hdc);
		SelectObject(memDC,pic);
		GetObject(pic,sizeof(bm),&bm);
		BitBlt(hdc,500,400,bm.bmWidth,bm.bmHeight,memDC,0,0,SRCCOPY);
		DeleteDC(memDC);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		DeleteObject(pic);
		PostQuitMessage(0);
		break;
	default:
		return DefScreenSaverProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	return FALSE;
}
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
	return TRUE;
}
The problem is that the MessageBox always pops up when I go to run the program showing that the bitmap didn't load, but yet the code compiles with no problem. Any ideas?

Share this post


Link to post
Share on other sites
IIRC:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)

You should be setting your global hInst variable to ^ this hInstance (::hInst = hInstance).

What mfawcett was saying is that if you have not correctly set the value of your global hInst first before you calling LoadBitmap, it'll fail. Make sure it has been set before you call it.

Share this post


Link to post
Share on other sites
zebdor    122
Thank you for both of your help! With it I figured out the problem. Instead of using the global hInst I simply removed that and added hMainInstance in LoadBitmap.

Thanks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this