【发布时间】:2013-02-06 22:13:42
【问题描述】:
我正在构建一个 MFC C++ 应用程序。我需要将窗口呈现为动态字符串的形状。为了存档,我做了以下操作:
- 使用 GDI+ 在 GDI+ 中使用
GraphicsPath和AddString呈现文本 - 从
GraphicsPath创建一个Region对象 - 将
Region转换为CRng并使用SetWindowRgn设置窗口形状
代码如下:
在OnInitDialog:
CClientDC dc(this);
Graphics graphics(dc.GetSafeHdc());
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
GraphicsPath path;
FontFamily fontFamily(L"Arial");
StringFormat strformat;
wchar_t pszbuf[] = L"testString";
path.AddString(pszbuf, wcslen(pszbuf), &fontFamily, FontStyleRegular, 14,
Gdiplus::Point(0,16), &strformat );
Region myRgn(&path);
CRng rgn;
rgn.FromHandle(myRgn.GetHRGN(&graphics));
SetWindowRgn(rgn,TRUE) ;
在OnPaint
RECT rect;
GetWindowRect(&rect);
CBrush brush;
brush.CreateSolidBrush(color);
paint_dc.FillRect(&rect, &brush);
问题是我没有看到任何显示。有cmets吗?
【问题讨论】:
-
您是否尝试过使用 LRESULT OnPaint (HWND hWnd) 和 BeginPaint / EndPaint?
-
根本没有错误检查,所以你当然不知道为什么它不起作用。 SetWindowRgn 并没有按照您的想法做,请改用 SelectObject()。
-
@HansPassant 你为什么认为没有错误检查?当然有。此处的清单是为了突出代码中的主要调用。我需要将整个窗口设置为区域的形状,所以我确实需要 SetWindowRgn(更新了描述)