Copy contents of one DeviceContext to another DeviceContext

独自空忆成欢 提交于 2019-12-11 11:41:53

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/10407500/copy-contents-of-one-devicecontext-to-another-devicecontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!