我正在运行以下代码,

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);

代码很好。但我在这个矩形周围看到黑色。这是为什么? 这是一个示例图像。

有帮助吗?

解决方案

位图很可能被初始化为全黑。然后,您将在 x 坐标 126 和 624 之间绘制一个白色矩形。因此,x=126 左侧和 x=624 右侧的所有内容都保持黑色。

编辑:这 的文档 CreateCompatibleBitmap 没有说明如何初始化位图,因此您应该使用特定颜色显式初始化位图,正如 Goz 建议的那样,使用 FillRect:

RECT rc;

rc.left=0;
rc.top=0;
rc.right=c.right-c.left;
rc.bottom=200;

FillRect(hdcMem, &rc, (HBRUSH)GetStockObject(GRAY_BRUSH));

此示例将位图填充为灰色 - 您可能需要 CreateSolidBrush 如果您需要不同的颜色,请使用自己的刷子。(别忘了打电话 DeleteObject 当你完成时。)

顺便说一句,我发现你的位图被设置为 200 的恒定高度有点奇怪——正常的事情是使位图的高度等于窗口的高度(就像为宽度)。

其他提示

也许这是因为你还没有初始化内存位图区域到给定的颜色?尝试FillRect'ing的背景,不同的颜色,然后画出你的白色矩形过目一下,看看会发生什么。

每MSDN http://msdn.microsoft.com/en-us /library/dd162898.aspx

  

在矩形是通过使用当前笔概述和通过使用当前刷填充。

考虑主叫FillRect代替,或调用Rectangle”之前选择合适的笔。

我使用:

    // Fill the background
    hdcMem->FillSolidRect(c, hdcMem->GetBkColor());

正如注释

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top