Bitmap not being painted in Win32 C++

708 views Asked by At

I have a window using Win32 and in the message handler I have a case for WM_PAINT, so that a bitmap is drawn in the window. However on running the bitmap is not drawn, is there something I am missing? Do I need to manually send the WM_PAINT message?

Here is the code I have: http://pastebin.com/bi48LB0U

and this is the WM_PAINT case:

case WM_PAINT:
    hDC = BeginPaint(hwnd, &ps);
    bmp = LoadBitmap(hInst, L"C:\\example.bmp");
    memDCExercising = CreateCompatibleDC(hDC);
    SelectObject(memDCExercising, bmp);
    BitBlt(hDC, 100, 100, 500, 500, memDCExercising, 0, 0, SRCCOPY);
    DeleteDC(memDCExercising);
    DeleteObject(bmp);
    EndPaint(hwnd, &ps);
    break;
1

There are 1 answers

0
IInspectable On BEST ANSWER

Your bitmap doesn't show, because your call to LoadBitmap returns NULL, due to an invalid lpBitmapName argument. From the documentation for LoadBitmap:

lpBitmapName [in]: A pointer to a null-terminated string that contains the name of the bitmap resource to be loaded. Alternatively, this parameter can consist of the resource identifier in the low-order word and zero in the high-order word. The MAKEINTRESOURCE macro can be used to create this value.

In other words: LoadBitmap can only load bitmaps from Resources of type RT_BITMAP (or predefined bitmaps provided by the system). If you need to load a bitmap from disk, use LoadImage instead.

If you need to load image data other than plain bitmap files, consider using the Windows Imaging Component.