Skip to main content
gamedev.net
🔒 Locked

Win API: Bitmaps

Started by Fixxer 17 replies 2.8k views
Original Post
Fixxer
Fixxer
I want to create a bitmap in photoshop and use it for my application as the background. How can I load a bitmap and display it using the windows API. An explanation or a link to one would be great. I already tried looking through MSDN but I couldnt find it.
Extrarius
Extrarius
Essentially, the process will be that you load the bitmap (LoadBitmap), create a memory-DC (GetDC on the window, then CreateCompatibleDC), select the bitmap into the memory-DC (SelectObject), then copy from the memory-dc to the window-dc (BitBlt or StretchBlt).
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Fixxer
Fixxer
How can I manually add a bitmap to a rc file because Im not using the IDE's resource editor and if i do it will screw up all the other resources.
Fixxer
Fixxer
yea right after i posted i went DUH! lol so ive got that, and ive gotten to the last function call. Im a bit confused on what the destination dc and what the source dc are.

What I have so far:
HANDLE hmainbg = LoadBitmap(hmaininstance,"test.bmp");HDC hmaindc = GetDC(hmainwnd);CreatCompatibleDC(hmaindc);SelectObject(hmaindc,hmainbg);


Confusing Prototype:
BOOL BitBlt(  HDC hdcDest, // handle to destination DC  int nXDest,  // x-coord of destination upper-left corner  int nYDest,  // y-coord of destination upper-left corner  int nWidth,  // width of destination rectangle  int nHeight, // height of destination rectangle  HDC hdcSrc,  // handle to source DC  int nXSrc,   // x-coordinate of source upper-left corner  int nYSrc,   // y-coordinate of source upper-left corner  DWORD dwRop  // raster operation code);
Colin Jeanne
Colin Jeanne
The source DC is the DC that contains the bitmap and the destination DC is the DC that you are going to paint to. Here is an example:

// Add this to your dialog's window procedure - it will be called when the// dialog needs to be repaintedcase WM_PAINT: {  PAINTSTRUCT ps;  // hmaindc will be the DC of your window  HDC hmaindc = BegainPaint(hwnd, &ps);    // hmemorydc will hold hmainbg so that we can paint it to the window    HDC hmemorydc = CreateCompatibleDC(hmaindc);    // Of course we have to load the bitmap itself    HBITMAP hmainbg = LoadBitmap(hmaininstance, "test.bmp");    // When we select hmainbg into hmemorydc we must hold the original    // bitmap (which is returned) so that we can return the DC to its original    // state before we delete it    HBITMAP holdbmp = (HBITMAP)SelectObject(hmemorydc, hmainbg);    // Before we draw the bitmap, let's get some information about it    // This includes its width and height    BITMAP bmpinfo;    GetObject(hmainbg, sizeof(bmpinfo), &bmpinfo);    // Now we can draw hmainbg to the window using BitBlt()    BitBlt(hmaindc, 0, 0, bmpinfo.bmWidth, bmpinfo.bmHeight,           hmemorydc, 0, 0, SRCCOPY);    // Now the painting is done so we begin to clean up    // First selelect the original bitmap into the memory DC    SelectObject(hmemorydc, holdbmp);    // Now delete the bitmap    DeleteObject(hmainbg);    // And then the memory DC    DeleteDC(hmemorydc);  // And finally, end the painting process  EndPaint(hwnd, &ps);  break;}
Extrarius
Extrarius
If you're loading the bitmap from a resource file, HANDLE hmainbg = LoadBitmap(hmaininstance,"test.bmp"); is incorrect. You only specify the file name if you're loading from a file. Otherwise, you have to use some macro (MAKEINTORESOURCE or MAKEINTRESOURCE or something like that)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Fixxer
Fixxer
This is what I have so far, no success.

		HBITMAP hmainbg = LoadBitmap(hmaininstance,"test.bmp");		HDC hmaindc = GetDC(hmainwnd);		HDC hmemdc = CreateCompatibleDC(hmaindc);		HBITMAP holdbmp = (HBITMAP)SelectObject(hmemdc,hmainbg);		BitBlt(hmemdc,0,0,325,300,hmaindc,0,0,SRCCOPY);		SelectObject(hmemdc, holdbmp);		DeleteObject(hmainbg);	        DeleteDC(hmemdc);
FreeTutorialNewbie
FreeTutorialNewbie
i have no clue if this will work..but here it goes

load the bitmap into the resource file
or have it available to be opened as a file

in the callback for messages put this in there

switch(msg){

...code in there already....

case WM_ERASEBKGND:
HBITMAP bmp=LoadBitmap(hinst,MAKEINTRESORCE(resource id for bitmap));
HDC hDCbg=(HDC)wParam;
RECT rc;
GetClientRect(hwnd,&rc);
HDC hdctemp=CreateCompatibleDC(hDCbg);
SelectObject(hdctemp,bmp);
BitBlt(hDCbg,rc.top,rc.left,rc.bottom-rc.top,rc.right-rc.left,hdctemp,0,0,SRCPAINT);

DeleteObject(hdctemp);
DeleteObject(bmp);
return 0;


that should work...some of the functions may be mixed up, but that is supposed to reapaint the background with a bitmap

to use a file and not a resource use
HBITMAP bmp=LoadBitmap(hinst,"c:/filename.bmp");

FreeTutorialNewbie
FreeTutorialNewbie
by the way..anybody know how to use the wave api to record?...
off topic i know

the WM_ERASEBKGROUND message is made for drawing the background
wparam is the hdc, beats haveing to fool with WM_PAINT and possibly replacing the entire window with your bitmap, after all you said background
WM_PAINT is to draw the entire window ( even the box, menu, everything)
Kippesoep
Kippesoep
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
Kippesoep
Kippesoep
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
Fixxer
Fixxer
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);
Kippesoep
Kippesoep
You must also include the header file in your actual C++ source file.
Kippesoep
Fixxer
Fixxer
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;
Kippesoep
Kippesoep
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
Extrarius
Extrarius
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

Topic Locked

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

Sign in to reply to this topic.