gpt4 book ai didi

windows - GDI GradientFill 不适用于屏幕外位图

转载 作者:可可西里 更新时间:2023-11-01 11:14:58 26 4
gpt4 key购买 nike

我正在尝试使用 GDI GradientFill 函数在离屏位图上绘制,然后通过 BitBlt 将其绘制到屏幕上。

但我总是得到一个黑色位图...如果我直接对屏幕使用 GradientFill,它就可以工作。

下面是一个示例应用程序,可以了解我的意思。

#pragma comment(lib, "msimg32.lib")
#include <windows.h>

const CHAR c_szWndClass[] = "GradientTestWnd";
const CHAR c_szWndTitle[] = "GradientTest";
const int c_nWndWidth = 1024;
const int c_nWndHeight = 768;

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof(wcx));
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcx.lpfnWndProc = DefWindowProc;
wcx.hInstance = hInstance;
wcx.lpszClassName = c_szWndClass;

RegisterClassEx(&wcx);

HWND hwndMain = CreateWindowEx(
0,
c_szWndClass,
c_szWndTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
c_nWndWidth,
c_nWndHeight,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hwndMain, SW_SHOW);

HDC hdc;
hdc = GetDC(hwndMain);

HDC hdcOffscreen = CreateCompatibleDC(hdc);
HBITMAP bitmap = CreateCompatibleBitmap(hdcOffscreen, c_nWndWidth, c_nWndHeight);
HBITMAP old_bitmap = (HBITMAP) SelectObject(hdcOffscreen, bitmap);

TRIVERTEX vertices[2];
ZeroMemory(&vertices, sizeof(vertices));
vertices[0].Red = 0xFF00;
vertices[0].Green = 0x0000;
vertices[0].Blue = 0x0000;
vertices[0].x = 0;
vertices[0].y = 0;

vertices[1].Red = 0x0000;
vertices[1].Green = 0x0000;
vertices[1].Blue = 0xFF00;
vertices[1].x = c_nWndWidth;
vertices[1].y = c_nWndHeight;

GRADIENT_RECT rects[1];
ZeroMemory(&rects, sizeof(rects));
rects[0].UpperLeft = 0;
rects[0].LowerRight = 1;

// This works
//GradientFill(hdc, vertices, 2, rects, 1, GRADIENT_FILL_RECT_V);

// This doesn't
GradientFill(hdcOffscreen, vertices, 2, rects, 1, GRADIENT_FILL_RECT_V);
BitBlt(hdc, 0, 0, c_nWndWidth, c_nWndHeight, hdcOffscreen, 0, 0, SRCCOPY);

Sleep(5000);

SelectObject(hdcOffscreen, old_bitmap);
DeleteObject(bitmap);
DeleteDC(hdcOffscreen);

return 0;
}

最佳答案

这里的问题实际上是由于您从中创建兼容位图的设备上下文的初始状态引起的 - 在这一行中:

HBITMAP bitmap = CreateCompatibleBitmap(hdcOffscreen, c_nWndWidth, c_nWndHeight);

hdcOffscreen 应该改为 hdc - 这是因为此处创建的设备上下文:

HDC hdcOffscreen = CreateCompatibleDC(hdc);

默认情况下选择了一个 1x1 单色位图 - 当您尝试从中创建兼容位图时,您也会得到一个单色位图。所以如果你这样做:

HBITMAP bitmap = CreateCompatibleBitmap(hdc, c_nWndWidth, c_nWndHeight);

应该看到您的渐变 :) 老问题似乎得到了回答,但我认为 id 只是帮助澄清它为什么不起作用!

详细信息/链接:

http://msdn.microsoft.com/en-us/library/dd183489%28VS.85%29.aspx

When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high

http://msdn.microsoft.com/en-us/library/dd183488%28v=VS.85%29.aspx

The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter

第 :)

关于windows - GDI GradientFill 不适用于屏幕外位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2146282/

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