-
Why won't my bitmap load
Heres all the code i have relating to my bitmap that won't load, anyone see whats wrong with it, i'm using Borland C++ 5.0
//Test.RC
TEST BITMAP DISCARDABLE "test.bmp"
//In CPP file.
static HBITMAP hBitmap;
static int cxClient, cyClient, cxSource, cySource;
BITMAP bitmap;
HDC hdc,hdcMem;
HINSTANCE hInstance;
int x,y;
PAINTSTRUCT ps;
case WM_CREATE:
{
hBitmap = LoadBitmap(hInstance,"TEST");
GetObject(hBitmap,sizeof(BITMAP), &bitmap);
cxSource = bitmap.bmWidth;
cySource = bitmap.bmHeight;
return(0);
}
case WM_SIZE:
{
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return(0);
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem,hBitmap);
for(y=0;y<cyClient;y+=cySource)
for(x=0;x<cxClient;x+=cxSource)
{
BitBlt(hdc,x,y,cxSource,cySource,hdcMem,0,0,SRCCOP Y);
}
DeleteDC(hdcMem);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
DeleteObject(hBitmap);
PostQuitMessage(0);
return(0);
} break;
-
Did you make hInstance global? If not you need to retrieve it in your WinProc. Use:
Code:
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
//rest of code
-
Thanks, that works, never could get that to compile before dunno why it works now
-
simple, you obtain the instance of you program in your WinMain function, however this is a local variable and goes out of scope when you leave that function. When you tried to load your bitmap, as a resource of your program with LoadBitmap(hInstance, "TEST"), you are telling the OS to look for a bitmap id of TEST in hInstance(your program). However since hInstance was never defined the OS did not know where to look, or actually it looked for it in whatever memory the unitialized handle(hInstance) held.
-
Do yourself a favor. Take this function and place it in your include files. Call it once in WinMain and forget about it. After being initialized, call this function without parameters whenever you need a handle to the current instance!
I got so tired of passing this useless variable around and I haven't missed it since!
ie:
int WINAPI WinMain(HINSTANCE hThisInstance, //...)
{
Instance( hThisInstance ); //...initialize once...
//...stuff
}
HBITMAP GetBitmap(char *name)
{
return LoadBitmap( Instance(), name ); //...get this instance...
}
Code:
HINSTANCE Instance( HINSTANCE hThisInstance = NULL )
{
static HINSTANCE globalInstance;
if(hThisInstance != NULL) globalInstance = hThisInstance;
return globalInstance;
}
//...or try the AI version:
HINSTANCE Instance( HINSTANCE hThisInstance = NULL )
{
static bool forgot = true;
static HINSTANCE globalInstance;
if(hThisInstance != NULL)
{
globalInstance = hThisInstance;
forgot = false;
}
if(forgot) MessageBox(NULL, "You Forgot To Call Me In WinMain ;)", "Hey, Mr. Programmer:", 0);
return globalInstance;
}