Reputation: 1322
My goal is to create a bitmap in memory and use its handle as a parameter of the BM_SETIMAGE message (a message that set a button's bitmap).
The following is my code:
........
HDC hdc = GetDC(hwnd);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, 100, 100);
HBITMAP hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);
Rectangle(memDC, 0, 0, 100, 100);
HBRUSH brush = CreateSolidBrush(RGB(0xff, 0xff, 0x00));
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = 100;
rc.bottom = 100;
FillRect(memDC, &rc, brush);
SendMessage(GetDlgItem(hDlg, IDC_SET_START_PAGE_BG), BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hMemBmp);
........
but it doesn't work, I just got black color on my button.
Questions:
Does FillRect(memDC, &rc, brush)
modify the content of hMemBmp? If not, what SelectObject(memDC, hMemBmp)
did for these 2 objects?
Should I use CreateBitmap
instead of CreateCompatibleBitmap
to do my task?
Upvotes: 0
Views: 1109
Reputation: 2313
You should select the old bitmap back into the memory DC before you send the message. According to MSDN a bitmap can be selected into only a single DC at a time.
Upvotes: 2