gpt4 book ai didi

C++ BitBlt 显示棋盘和偏色

转载 作者:太空狗 更新时间:2023-10-29 22:53:57 25 4
gpt4 key购买 nike

我正在尝试绘制到屏幕外设备上下文/位图,并使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com