C++ BitBlt 显示棋盘和偏色

标签 c++ winapi bitmap gdi bitblt

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

enter image description here

左边的蓝色、黄色和绿色条被直接绘制到窗口的 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(与窗口兼容),创建一个兼容的位图,选择它,绘制矩形,然后使用 SRCCOPY 位 block 传输。所有这些在我看来都是正确的。

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

最佳答案

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

关于C++ BitBlt 显示棋盘和偏色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091782/

相关文章:

c - 获取硬盘的簇大小(通过代码)

windows - 为什么我要使用无限超时的 Sleep()?

delphi - 在调用 Canvas.DrawBitmap (Firemonkey) 之前对位图应用转换

c++ - openmp:增加线程数会降低性能

c++ - 如何防止 Gnome 在执行 alt-tab 时显示两个窗口? (C++ qt 应用程序)

c++ - 如何在 CLion 中启用 C++11?

c++ - ActiveX 控件的全屏模式

android - ImageView 位图缩放尺寸

android - 获取 Facebook 个人资料图片时 Bitmap 为 null

c++ - 当字符串表示零时使用atoi?