Gdiplus::PathGradientBrush 在 MFC 应用程序中不绘制

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

"simple" 直接代码,主要是 article in MSDN 的复制粘贴,不会产生任何输出。我添加了更多的线条(使用普通 GDI 绘制)以确保坐标正确 - 这些线条工作得很好。

代码正在更改 STATIC 控件的背景,绘图在 OnPaint 处理程序中执行。对于此 STATIC 控件,OnCtlColor 被扩充为 return 和 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 documentation,用作画笔构造函数的第三个默认参数,默认值 WrapModeClamp 表示:

WrapModeClamp
Specifies that no tiling takes place.

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

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

使用画笔的 TranslateTransform 对其进行适当偏移,使其绘制出预期的图案!在这种情况下,无需指定平铺,因为它变得不必要了。