painting/loading 2 diffrent bitmaps

Started by
4 comments, last by violetann 18 years, 12 months ago
ok i've been playing around with loading bitmaps, in windows app, i've got one to load fine but the second is tricky it keeps loading the first image instead of the first, i think it's to do with the "HBITMAP hbmOld = SelectObject(hdcMem, g_hbmBall);" if i change the g_hbmBall to g_hbmcup it loads the cup image in the two places. can anyone tell me how to get it to load the two diffrent ones or have an address to somewhere which dose? case WM_CREATE: g_hbmBall = LoadBitmap(GetModuleHandle(NULL), KEINTRESOURCE(IDB_BALL)); g_hbmcup = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_cup)); if(g_hbmBall == NULL) MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION); break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_PAINT: { // Just a note, never use a MessageBox from inside WM_PAINT // The box will cause more WM_PAINT messages and you'll probably end up // stuck in a loop BITMAP bm; PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); HDC hdcMem = CreateCompatibleDC(hdc); HBITMAP hbmOld = SelectObject(hdcMem, g_hbmBall); GetObject(g_hbmBall, sizeof(bm), &bm); BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); GetObject(g_hbmcup, sizeof(bm), &bm); BitBlt(hdc, 20, 20, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); SelectObject(hdcMem, hbmOld); DeleteDC(hdcMem); EndPaint(hwnd, &ps); } break; case WM_DESTROY: DeleteObject(g_hbmBall); DeleteObject(g_hbmcup); PostQuitMessage(0); break;
Advertisement
Well, you've almost answered your own question. You are selecting the ball bitmap into your memory hdc. So, when you call BitBlt, that's the image it's going to blit. You need to break it down into two steps. First, select the ball bitmap into hdcMem and BitBlt. Then, select the cup bitmap into hdcMem and BitBlt again.
Quote:Original post by Anonymous Poster
Well, you've almost answered your own question. You are selecting the ball bitmap into your memory hdc. So, when you call BitBlt, that's the image it's going to blit. You need to break it down into two steps. First, select the ball bitmap into hdcMem and BitBlt. Then, select the cup bitmap into hdcMem and BitBlt again.


That was me. I got logged out unexpectedly.
humm so something like:

HBITMAP hbmOld = SelectObject(hdcMem, g_hbmBall);
GetObject(g_hbmBall, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);


HBITMAP hbmOld = SelectObject(hdcMem, g_hbmcup);
GetObject(g_hbmcup, sizeof(bm), &bm);
BitBlt(hdc, 20, 20, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

i'll have to double check when i get home on my compiler.
Well, actually:

HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);GetObject(g_hbmBall, sizeof(bm), &bm);BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);//No need to store return value here as it's stored aboveSelectObject(hdcMem, g_hbmcup);GetObject(g_hbmcup, sizeof(bm), &bm);BitBlt(hdc, 20, 20, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);//Restore old valueSelectObject (hdcMem, hbmOld);
Kippesoep
yey it works, thanks :)

This topic is closed to new replies.

Advertisement