【发布时间】:2009-10-20 18:11:42
【问题描述】:
我正在运行以下代码,
HDC hdc;
HDC hdcMem;
HBITMAP bitmap;
RECT c;
GetClientRect(viewHandle, &c);
// instead of BeginPaint use GetDC or GetWindowDC
hdc = GetDC(viewHandle);
hdcMem = CreateCompatibleDC(hdc);
// always create the bitmap for the memdc from the window dc
bitmap = CreateCompatibleBitmap(hdc,c.right-c.left,200);
SelectObject(hdcMem, bitmap);
// only execute the code up to this point one time
// that is, you only need to create the back buffer once
// you can reuse it over and over again after that
// draw on hdcMem
// for example ...
Rectangle(hdcMem, 126, 0, 624, 400);
// when finished drawing blit the hdcMem to the hdc
BitBlt(hdc, 0, 0, c.right-c.left,200, hdcMem, 0, 0, SRCCOPY);
// note, height is not spelled i before e
// Clean up - only need to do this one time as well
DeleteDC(hdcMem);
DeleteObject(bitmap);
ReleaseDC(viewHandle, hdc);
代码很好。但我在这个矩形周围看到黑色。这是为什么? Here is an example image.
【问题讨论】:
-
您应该保存 SelectObject 的结果并在删除之前恢复它,以避免 Windows 问题,您可能会因为 DC 的发布而避免崩溃,但它可能有其他副作用: HBITMAP 保存BM; .. saveBM = SelectObject(hdcMem, bitmap); ... SelectObject(hdcMem, saveBM);删除对象(位图);