From 990e32a9551c211e868e77d132df9c54361574af Mon Sep 17 00:00:00 2001 From: zhongyang219 Date: Sun, 12 Jan 2025 20:24:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BD=BF=E7=94=A8GDI+?= =?UTF-8?q?=E7=9A=84DrawWindowText=E5=87=BD=E6=95=B0=E4=B8=AD=E5=8F=82?= =?UTF-8?q?=E6=95=B0multi=5Fline=E5=92=8Cdraw=5Fback=5Fground=E6=97=A0?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TrafficMonitor/DrawCommonEx.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/TrafficMonitor/DrawCommonEx.cpp b/TrafficMonitor/DrawCommonEx.cpp index 9b3d00b4..31c6054e 100644 --- a/TrafficMonitor/DrawCommonEx.cpp +++ b/TrafficMonitor/DrawCommonEx.cpp @@ -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; @@ -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); } @@ -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); }