Gdiplus::P mathGradientBrush in a MFC 应用程序不绘制

Gdiplus::PathGradientBrush in an MFC application doesn't draw

本文关键字:应用程序 绘制 MFC in mathGradientBrush Gdiplus      更新时间:2023-10-16

一个"简单"的直接代码,主要是从 MSDN 中的文章复制粘贴,不会产生任何输出。我添加了更多线条(使用普通 GDI 绘制)以确保坐标正确 - 这些线条工作正常。

代码正在更改STATIC控件的背景,绘图在OnPaint处理程序中执行。OnCtlColor已扩充以返回此STATIC控件的NULL_BRUSH,因此框架不会完成绘制。

检查 GDI+ 对象和方法的状态(在调试器中和通过日志记录)显示一切正常。我不知所措,请帮忙解决这个难题。

以下是相关代码(删除了错误处理和 static_cast 的 \W4):

CPaintDC dc(this);
Graphics graphics(dc);
CRect rc;
GetDlgItem(IDC_STATIC_GS_COLOR)->GetWindowRect(&rc); // the STATIC to paint
ScreenToClient(&rc);
// GDI+ uses real numbers, not integers
const Rect gdirc = Rect(rc.left, rc.top, rc.Width(), rc.Height());
// next 2 lines work just fine - painting a RED background
const SolidBrush solidBrush((ARGB)Color::Red);
graphics.FillRectangle(&solidBrush, gdirc);
const PointF sz(rc.Width() - 1, rc.Height() - 1);
PointF pts[] = {PointF(0, 0), PointF(sz.X, 0), PointF(sz.X, sz.Y), PointF(0, sz.Y)};
PathGradientBrush brush(pts, 4);
Color colors[] = {Color(0, 0, 0), Color(255, 0, 0), Color(0, 255, 0), Color(0, 0, 255)};
int count = 4;
brush.SetSurroundColors(colors, &count);
brush.SetCenterColor(Color(128, 128, 128));
// !!!!! this doesn't fail, returns a status OK, and has NO EFFECT on the image :(
const auto status = graphics.FillRectangle(&brush, gdirc);
// this draws a smaller inner GREEN rectangle, works as expected
rc.DeflateRect(20, 20);
dc.FillSolidRect(&rc, m_rgb);
根据有关

WrapMode枚举的 MSDN 文档(用作画笔构造函数的第三个默认参数),默认值WrapModeClamp表示:

包装模式夹具
指定不进行切片。

一旦我将第三个参数添加到构造函数并指定了WrapModeTile值,绘画就开始发生。

Gdiplus::PathGradientBrush brush(pts, 4, WrapModeTile);

或者使用画笔的TranslateTransform正确偏移它,以便它绘制出预期的图案!在这种情况下,无需指定切片,因为它变得不必要。