Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with bitmaps

Started by zebdor Nov 29, 2005 at 4:03 PM 3 replies 550+ views
Original Post
zebdor
zebdor
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?
mfawcett
mfawcett
Your hInstance is probably not set (or set incorrectly) when you call LoadBitmap.
--Michael Fawcett
zebdor
zebdor
How should it be set?
load_bitmap_file
load_bitmap_file
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.
zebdor
zebdor
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.