GDI GradientFill 不适用于屏幕外位图
我正在尝试使用 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;
}
I am trying to use the GDI GradientFill function to draw on a offscreen bitmap, then BitBlt that to the screen.
But I always get a black bitmap... if I GradientFill directly to the screen it works.
Below is a sample app to see what I mean.
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题实际上是由于您从中创建兼容位图的设备上下文的初始状态 - 在这一行中:
hdcOffscreen 应改为 hdc - 这是因为此处创建的设备上下文:
默认选择了一个 1x1 单色位图进入它 - 当您尝试从中创建兼容的位图时,您也会获得单色位图。所以如果你这样做:
你应该看到你的渐变:)老问题似乎得到了回答,但我认为id只是帮助澄清为什么它不起作用!
详细信息/链接:
http://msdn.microsoft.com /en-us/library/dd183489%28VS.85%29.aspx
http://msdn.microsoft。 com/en-us/library/dd183488%28v=VS.85%29.aspx
hth 标识:)
the problem here is actually due to the initial state of the device context from which you are creating the compatible bitmap - in this line:
hdcOffscreen should instead be hdc - this is because the device context created here:
has by default a 1x1 monochrome bitmap selected into it - when you attempt to create a compatible bitmap from that, you will get a monochrome bitmap also. so if you do this instead:
you should see your gradient :) old question that appears to be answered but i thought id just help clarify on exactly why its not working!
details/links:
http://msdn.microsoft.com/en-us/library/dd183489%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/dd183488%28v=VS.85%29.aspx
hth :)
创建 DIB 而不是兼容位图。
替换
为
Create a DIB instead of a compatible bitmap.
Replace
with