1

I've never done any GDI programming and despite taking several shots in the dark and searching the documentation I haven't found the correct way to do copy the contents of one DC to another DC.

The code I have at the moment is below. I don't understand why it's not working (the window remains just remains blank after creation).

SIZE srcSize;
// ... Get size of source DC

HDC destDC = ...;   // from GetDC(myWindow), myWindow was
                    // sized before this to properly contain source
HDC sourceDC = ...;

HBITMAP buffer = CreateCompatibleBitmap(sourceDC, srcSize.cx, srcSize.cy);
HGDIOBJ oldObj = SelectObject(destDC, buffer);
BitBlt(destDC, 0, 0, srcSize.cx, srcSize.cy, sourceDC, 0, 0, SRCCOPY);
SelectObject(destDC, oldObj);
DeleteObject(buffer);

//... ReleaseDC()s here

What's the proper way this is done?

8
  • One thing maybe i should add is that I'm not calling this from the WM_PAINT message, and don't handle that in the WindowProcedure, should I override that? It might be that the DefWindowProc handler for WM_PAINT just paints the window white after I copy.
    – cplusplus
    May 2, 2012 at 3:59
  • 1
    Yes, this should be in WM_PAINT.
    – chris
    May 2, 2012 at 6:16
  • @cplusplus, yes of course you need to paint the window in WM_PAINT or the defwindowproc will paint it for you... You also need to revalidate the window, to prevent it being called multiple times.
    – Ben
    May 2, 2012 at 9:18
  • Also you need to actually have something on the bitmap you copy from.... If you copy an empty bitmap you are not going to see anything. Also I assume SourceDC is a memory DC???
    – Ben
    May 2, 2012 at 9:20
  • 1
    Why are you trying to select a bitmap into the destination DC? Can't you just Blt directly onto the DC? Also consider creating a memory DC and associated bitmap, and keeping it permanently. When the DD surface changes you can copy it to the memory DC/bitmap and in WM_PAINT you can copy it from the memory DC to the window DC.
    – Ben
    May 2, 2012 at 20:03

2 Answers 2

2

The only thing necessary to copy from one DC to another is a BitBlt. Code that works is below.

SIZE srcSize;
// ... Get size of source DC

HDC destDC = ...;   // from GetDC(myWindow), myWindow was
                    // sized before this to properly contain source
HDC sourceDC = ...;

BitBlt(destDC, 0, 0, srcSize.cx, srcSize.cy, sourceDC, 0, 0, SRCCOPY);
//... ReleaseDC()s here
0

It's not very clear to me what you are trying to do. First off, why create the new bitmap and select it into the window (sorry, "client area") DC? All you want is paint/draw the window, isn't it? This is not needed then. The destDC is exactly the window's client area surface.

Does sourceDC really contain anything? For example, does it have a bitmap slected into it?

And of course, you SHOULD process WM_PAINT. If you process this message the window is validated, and you are not required to validate it explicitly. Using GetDC()/ReleaseDC() is called "drawing", as opposed to "painting". In an application I made in the past I had to use both methods, painting (processing WM_PAINT) for responding to resizing, exiting from minimized state and bringing the window to foreground (if previously obscured by another) and drawing, for making certain changes immediately visible (instead of invalidating the window and waiting for the application to nearly enter the idle state first - pls note that WM_PAINT is a low-priority message).

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.