C++ BitBlt 显示棋盘和偏色

C++ BitBlt displaying checkerboard and skewed colors

我正在尝试绘制到屏幕外设备上下文/位图并使用 bitblt 将图像移动到主 hdc。这是我目前看到的结果:

左边的蓝色、黄色和绿色条直接绘制到 window 的 hdc。右边看起来很奇怪的那些被绘制到后台缓冲区并作为单个帧复制过来。它们应该是相同的,但显然不是这样。

这是我正在使用的代码,简化为一个最小示例:

COLORREF color_yellow = RGB (224, 224, 0);
COLORREF color_green = RGB (0, 192, 0);
COLORREF color_blue = RGB (0, 0, 192);

HBRUSH brush_yellow = CreateSolidBrush (color_yellow);
HBRUSH brush_green = CreateSolidBrush (color_green);
HBRUSH brush_blue = CreateSolidBrush (color_blue);

HDC hdc = GetDC (Window);
HDC hdc_buffer = CreateCompatibleDC (hdc);
HBITMAP bitmap_buffer = CreateCompatibleBitmap (hdc_buffer, blit.screen_width, blit.screen_height);
SelectObject (hdc_buffer, bitmap_buffer);

draw_rectangle (hdc, 0, 0, 100, 30, brush_blue);
draw_rectangle (hdc, 0, 30, 100, 60, brush_yellow);
draw_rectangle (hdc, 0, 60, 100, 90, brush_green);

draw_rectangle (hdc_buffer, 0, 0, 100, 30, brush_blue);
draw_rectangle (hdc_buffer, 0, 30, 100, 60, brush_yellow);
draw_rectangle (hdc_buffer, 0, 60, 100, 90, brush_green);

BitBlt (hdc, 120, 0, 100, 90, hdc_buffer, 0, 0, SRCCOPY);

void draw_rectangle (HDC hdc, int left, int top, int right, int bottom, HBRUSH brush)
  {
  RECT rect;
  SetRect (&rect, left, top, right, bottom);
  FillRect (hdc, &rect, brush);
  }

我正在创建一个新的 hdc(与 window 兼容),创建一个兼容的位图,选择它,绘制矩形,然后使用 SRCCOPY 进行位图传输。所有这些对我来说都是正确的。

我确定有一些小事我没有做,但我找不到。

这在 CreateCompatibleBitmap 的文档中有解释:

Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context

因此,更改

CreateCompatibleBitmap(hdc_buffer, width, height);//monochrome

CreateCompatibleBitmap(hdc, width, height);//colored bitmap