我正在尝试使用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;
}
答案 0 :(得分:4)
这里的问题实际上是由于您正在创建兼容位图的设备上下文的初始状态 - 在此行中:
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
创建内存DC时,它 显示表面恰好是一个 单色像素宽和一个 单色像素高
http://msdn.microsoft.com/en-us/library/dd183488%28v=VS.85%29.aspx
创建的位图的颜色格式 通过CreateCompatibleBitmap函数 匹配设备的颜色格式 由hdc参数
标识
hth:)
答案 1 :(得分:1)
创建DIB而不是兼容的位图。
替换
HBITMAP bitmap = CreateCompatibleBitmap(hdcOffscreen, c_nWndWidth, c_nWndHeight);
与
BITMAPINFO BitmapInfo;
memset(&BitmapInfo, 0, sizeof(BITMAPINFOHEADER));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = c_nWndWidth;
BitmapInfo.bmiHeader.biHeight = c_nWndHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP bitmap = CreateDIBSection(hdcOffscreen, &BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);