-1

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;
Pottsiex5
  • 487
  • 2
  • 6
  • 19
  • 1
    `assert(bmp != NULL);` – Mohit Jain Jun 19 '15 at 09:21
  • 2
    Doomed to fail, LoadBitmap() takes the resource ID, not a path name. You need LoadImage() instead. And yes, always check winapi return values, assert() at a minimum. – Hans Passant Jun 19 '15 at 09:28
  • How can I make a resource ID from my file name? – Pottsiex5 Jun 19 '15 at 09:28
  • [LoadBitmap](https://msdn.microsoft.com/en-us/library/dd145033.aspx): *"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."* – IInspectable Jun 19 '15 at 09:29
  • You're asking the wrong question: There is no way to create a resource ID for an file residing on disk. Resource IDs are available for resources compiled into a binary image only. If you want to load a file from disk, use [LoadImage](https://msdn.microsoft.com/en-us/library/ms648045.aspx) instead. – IInspectable Jun 19 '15 at 09:31
  • 1
    You really truly do not want to load an image from disk in `WM_PAINT`. That can be called a lot. Load it once and hold on to the HBITMAP. – David Heffernan Jun 19 '15 at 09:31
  • I have it working from loading the resource once. Would I be able to modify the resource and reload it at runtime? My program wants to be able to display the original image and a modified image... – Pottsiex5 Jun 19 '15 at 09:45
  • You do not seem to understand what the term [Resources](https://msdn.microsoft.com/en-us/library/ff468900.aspx) refers to, when used with Windows binaries. – IInspectable Jun 19 '15 at 09:47
  • On a side note, you are leaking the `HBITMAP` returned by `SelectObject()`. You need to save and restore it before calling `DeleteDC()`: `HBITMAP oldBmp = (HBITMAP) SelectObject(memDCExercising, bmp); ... SelectObject(memDCExercising, oldBmp); DelectDC(memDCExercising);` – Remy Lebeau Jun 20 '15 at 08:45

1 Answers1

1

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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181