Skip to content

Commit

Permalink
修正使用GDI+的DrawWindowText函数中参数multi_line和draw_back_ground无效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jan 12, 2025
1 parent 7190fc8 commit 990e32a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions TrafficMonitor/DrawCommonEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ void CDrawCommonEx::SetBackColor(COLORREF back_color, BYTE alpha)

void CDrawCommonEx::DrawWindowText(CRect rect, LPCTSTR lpszString, COLORREF color, Alignment align, bool draw_back_ground, bool multi_line, BYTE alpha)
{
//矩形区域
Gdiplus::RectF rect_gdiplus = CGdiPlusHelper::CRectToGdiplusRect(rect);

//绘制背景
if (draw_back_ground)
{
Gdiplus::SolidBrush brush(m_back_color);
m_pGraphics->FillRectangle(&brush, rect_gdiplus);
}
//设置字体
Gdiplus::Font font(m_pDC->GetSafeHdc());
//设置文本颜色
Gdiplus::SolidBrush brush(CGdiPlusHelper::COLORREFToGdiplusColor(color));
Gdiplus::SolidBrush brush(CGdiPlusHelper::COLORREFToGdiplusColor(color, alpha));
//设置对齐方式
Gdiplus::StringFormat format;
Gdiplus::StringAlignment alignment = Gdiplus::StringAlignmentNear;
Expand All @@ -57,10 +66,13 @@ void CDrawCommonEx::DrawWindowText(CRect rect, LPCTSTR lpszString, COLORREF colo
else if (align == Alignment::RIGHT)
alignment = Gdiplus::StringAlignmentFar;
format.SetAlignment(alignment); //水平对齐方式
format.SetFormatFlags(Gdiplus::StringFormatFlagsNoFitBlackBox);
format.SetLineAlignment(Gdiplus::StringAlignmentCenter); //垂直对齐方式
//矩形区域
Gdiplus::RectF rect_gdiplus = CGdiPlusHelper::CRectToGdiplusRect(rect);
UINT flags = Gdiplus::StringFormatFlagsNoFitBlackBox;
if (!multi_line)
flags |= Gdiplus::StringFormatFlagsNoWrap; //不自动换行
format.SetTrimming(Gdiplus::StringTrimmingNone); //禁止文本截断
format.SetFormatFlags(flags);

//绘制文本
m_pGraphics->DrawString(lpszString, -1, &font, rect_gdiplus, &format, &brush);
}
Expand All @@ -73,7 +85,7 @@ void CDrawCommonEx::SetDrawRect(CRect rect)
void CDrawCommonEx::FillRect(CRect rect, COLORREF color, BYTE alpha)
{
Gdiplus::RectF rect_gdiplus = CGdiPlusHelper::CRectToGdiplusRect(rect);
Gdiplus::SolidBrush brush(CGdiPlusHelper::COLORREFToGdiplusColor(color));
Gdiplus::SolidBrush brush(CGdiPlusHelper::COLORREFToGdiplusColor(color, alpha));
m_pGraphics->FillRectangle(&brush, rect_gdiplus);
}

Expand Down

0 comments on commit 990e32a

Please sign in to comment.