🔒 Locked
Win API: Bitmaps
Advertisement
Original Post
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:
Confusing Prototype:
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);
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;}
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)
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);
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");
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");
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)
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)
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);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.
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);
ah ok :-P
Here is the updated code, still no bitmap.
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:
(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).
//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).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement