[win32 GDI] Drawing a bitmap

Started by
3 comments, last by sipickles 17 years, 2 months ago
OK, mostly being a C++/DX programmer, my win 32 GDi skills are lacking and rusty. I am building a tool which I am using simple windows GDI stuff with, edit boxes, static controls etc. Nothing too fancy. Its a heightmap building tool and I'd like to be able to preview the bmp output as the tool runs. How do I display a bitmap in my window? I gather a bitmap is a STATIC control with SS_BITMAP style, and the handle of the bitmap in question. This makes the static control resize to the size of the bitmap, so we are getting somewhere. But how do I draw the bitmap? All the functions I've seen online involve catching the message WM_PAINT, and even I know that if you do that, nothing else will draw! So I need a way of updating the bitmap and presenting it. Please help! Thank you Simon
Advertisement
You dont need a static control to display a bitmap. In your window's WM_PAINT handler you can use BitBlt() to display the bitmap. Be sure to check out Winprog's primer.
using WM_PAINT is all very well, but as soon as I add

case WM_PAINT:     .....     break;


... to the code, all the other controls don't draw.

This I expected, since by specifying WM_PAINT you take responsibility for ALL painting from windows.

I just wanna have a bitmap which is updated. Not to have to manually draw every EDITBOX etc
When you handle WM_PAINT you have to be sure to call BeginPaint, and EndPaint when you have finished. If you don't do this you will have unpredictable results, like you are seeing now.

By handling the WM_PAINT message you are not taking responsibility for painting ALL windows, just the window for which you are handling the message - you are responsible for doing it properly though ;)
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
I agree, its only the single window which is affected by accepting WM_PAINT.

My problem is, this window has buttons and editboxes, as well as a bitmap. By using WM_PAINT to attempt to draw the bitmap, all the other buttons and editboxes are not painted!

from a tutorial:
    case WM_PAINT:    {        BITMAP bm;        PAINTSTRUCT ps;        HDC hdc = BeginPaint(hwnd, &ps);        HDC hdcMem = CreateCompatibleDC(hdc);        HBITMAP hbmOld = SelectObject(hdcMem, g_hbm);        GetObject(g_hbm, sizeof(bm), &bm);        BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);        SelectObject(hdcMem, hbmOld);        DeleteDC(hdcMem);        EndPaint(hwnd, &ps);    }    break;

This topic is closed to new replies.

Advertisement