Win API: Bitmaps

Started by
17 comments, last by GameDev.net 18 years, 7 months ago
HBITMAP hmainbg = LoadBitmap(hmaininstance,"test.bmp");

Possibly incorrect, unless you have also given the bitmap the resource name "test.bmp" (which is quite possible). If you have given it a proper ID, use MAKEINTRESOURCE instead. Anyway, if it fails to load, that should be easy to tell because hmainbg will be NULL.

BitBlt(hmemdc,0,0,325,300,hmaindc,0,0,SRCCOPY);

Probably incorrect. You probably mean to copy from the bitmap to the screen, not the other way around (which is actually possible, resulting in the bitmap being changed to match what was on the screen). I think you mean:
BitBlt(hmaindc,0,0,325,300,hmemdc,0,0,SRCCOPY);

Kippesoep
Advertisement
Quote:Original post by FreeTutorialNewbie
by the way..anybody know how to use the wave api to record?...
off topic i know

Then you should start another topic. The Wave API is really simple BTW, simply visit MSDN, check out this page.

Quote:WM_PAINT is to draw the entire window ( even the box, menu, everything)

No, just the client area. The other stuff is drawn in response to the WM_NCPAINT (non-client paint) message.
Kippesoep
I have it in a resource file, and Ive defined ID_BGBMP in a header that is included in the resource file, but when i do this I get an error saying that ID_BGBMP is undefined.

		HBITMAP hmainbg = LoadBitmap(hmaininstance,MAKEINTRESOURCE(ID_BGBMP));		HDC hmaindc = GetDC(hmainwnd);		HDC hmemdc = CreateCompatibleDC(hmaindc);		HBITMAP holdbmp = (HBITMAP)SelectObject(hmemdc,hmainbg);		BitBlt(hmaindc,0,0,325,300,hmemdc,0,0,SRCCOPY);		SelectObject(hmemdc, holdbmp);		DeleteObject(hmainbg);	        DeleteDC(hmemdc);
You must also include the header file in your actual C++ source file.
Kippesoep
ah ok :-P

Here is the updated code, still no bitmap.

case WM_ERASEBKGND:	{		HBITMAP hmainbg = LoadBitmap(hmaininstance,MAKEINTRESOURCE(ID_BGBMP));		HDC hmaindc = GetDC(hmainwnd);		HDC hmemdc = CreateCompatibleDC(hmaindc);		HBITMAP holdbmp = (HBITMAP)SelectObject(hmemdc,hmainbg);		BitBlt(hmaindc,0,0,325,300,hmemdc,0,0,SRCCOPY);		SelectObject(hmemdc, holdbmp);		DeleteObject(hmainbg);	    DeleteDC(hmemdc);	}break;
FreeTutorialNewbie was wrong about the specifics of WM_ERASEBKGND/WM_PAINT and you should put this code in response to WM_PAINT. Something like this:

//Take this outside the winprocHBITMAP hmainbg = LoadBitmap(hmaininstance,MAKEINTRESOURCE(ID_BGBMP));HDC hmemdc = CreateCompatibleDC(NULL);HBITMAP holdbmp = (HBITMAP)SelectObject(hmemdc, hmainbg);//Inside the winproc, make sure hmemdc is available to it (probably by making it a global)case WM_PAINT:{	PAINTSTRUCT ps;	HDC hdc = BeginPaint (hwnd, &ps);	BitBlt(hdc,0,0,325,300,hmemdc,0,0,SRCCOPY);	EndPaint (hwnd, &ps);}break;//Take this outside, too, making sure it executes at the end of the programSelectObject(hmemdc, holdbmp);DeleteObject(hmainbg);DeleteDC(hmemdc);


(BTW, although I've eliminated GetDC, you should make sure that when you do use GetDC you should also call ReleaseDC on it or you'll drain the resource pool and eventually kill the system).
Kippesoep
Quote:Original post by FreeTutorialNewbie
by the way..anybody know how to use the wave api to record?...
off topic i know[...]
The waveIn* (waveInOpen, etc) functions are well documented on MSDN.

"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
newbie on public computer

i went off of the msdn docs about the WM_ERASEBKGROUND
thats provides the hdc in the wparam
called when the background needs to be painted

the docs only set a solid brush background, but i figured since you are provided the hdc it could be used to draw a background?

excuse me if im wrong, but this is the beginners forums..lol
i am that guy^

oh yea...it is a better idea to load the bitmaps at window creation to ensure flickerless redraw

loading them everytime you repaint would be slower, so would putting the bitmap directly into the destination HDC

you can do this either at the beginning of winmain or in the initiate wm message as long as you remember to delete them at the closing of the window if you put it in the winproc, or at winmains termination if you put it there

This topic is closed to new replies.

Advertisement