Win API: Bitmaps

This topic is 4560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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).

Share on other sites
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.

Share on other sites
mybitmapname BITMAP bmpfilepath.bmp

Share on other sites
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);

Share on other sites
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;}

Share on other sites
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)

Share on other sites
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);

Share on other sites
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){

case WM_ERASEBKGND:
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

Share on other sites
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)

Share on other sites
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);

Share on other sites
Quote:
 Original post by FreeTutorialNewbieby 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.

Share on other sites
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);

Share on other sites
You must also include the header file in your actual C++ source file.

Share on other sites
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;

Share on other sites
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).

Share on other sites
Quote:
 Original post by FreeTutorialNewbieby 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.

Share on other sites
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

Share on other sites
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