user565739
user565739

Reputation: 1322

Create a bitmap in memory and use it

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:

  1. Does FillRect(memDC, &rc, brush) modify the content of hMemBmp? If not, what SelectObject(memDC, hMemBmp) did for these 2 objects?

  2. Should I use CreateBitmap instead of CreateCompatibleBitmap to do my task?

Upvotes: 0

Views: 1109

Answers (1)

HerrJoebob
HerrJoebob

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

Related Questions