|
1 | 1 | #include "stdafx.h" |
2 | 2 | #include "DrawCommonEx.h" |
3 | | - |
| 3 | +#include "DrawCommon.h" |
4 | 4 |
|
5 | 5 | CDrawCommonEx::CDrawCommonEx(CDC* pDC) |
6 | 6 | { |
7 | | - m_pDC = pDC; |
8 | | - hThm = OpenThemeData(::GetDesktopWindow(), L"Window"); |
| 7 | + Create(pDC); |
| 8 | +} |
| 9 | + |
| 10 | +CDrawCommonEx::CDrawCommonEx() |
| 11 | +{ |
9 | 12 | } |
10 | 13 |
|
11 | 14 |
|
12 | 15 | CDrawCommonEx::~CDrawCommonEx() |
13 | 16 | { |
14 | | - CloseThemeData(hThm); |
| 17 | + SAFE_DELETE(m_pGraphics); |
15 | 18 | } |
16 | 19 |
|
17 | | -void CDrawCommonEx::DrawWindowText(CRect rect, LPCTSTR str, COLORREF color) |
| 20 | +void CDrawCommonEx::Create(CDC* pDC) |
18 | 21 | { |
19 | | - m_pDC->SelectObject(m_pFont); |
20 | | - m_pDC->SetTextColor(color); |
| 22 | + ASSERT(pDC != nullptr); |
| 23 | + m_pDC = pDC; |
| 24 | + SAFE_DELETE(m_pGraphics); |
| 25 | + m_pGraphics = new Gdiplus::Graphics(pDC->GetSafeHdc()); |
| 26 | +} |
21 | 27 |
|
22 | | - DTTOPTS dttopts{}; |
23 | | - dttopts.dwSize = sizeof(DTTOPTS); |
| 28 | +void CDrawCommonEx::SetFont(CFont * pFont) |
| 29 | +{ |
| 30 | + //将字体设置到CDC,绘图时从CDC创建GDI+字体 |
| 31 | + m_pDC->SelectObject(pFont); |
| 32 | +} |
24 | 33 |
|
25 | | - dttopts.dwFlags = DTT_GLOWSIZE | DTT_COMPOSITED; //设置选项 |
26 | | - dttopts.iGlowSize = 0; //发光的范围大小 |
27 | | - HRESULT hr = DrawThemeTextEx(hThm, m_pDC->GetSafeHdc(), TEXT_LABEL, 0, str, -1, DT_LEFT | DT_SINGLELINE, rect, &dttopts); |
| 34 | +void CDrawCommonEx::DrawImage(Gdiplus::Image* pImage, CPoint start_point, CSize size, StretchMode stretch_mode) |
| 35 | +{ |
| 36 | + m_pGraphics->SetInterpolationMode(Gdiplus::InterpolationMode::InterpolationModeHighQuality); |
| 37 | + DrawCommonHelper::ImageDrawAreaConvert(CSize(pImage->GetWidth(), pImage->GetHeight()), start_point, size, stretch_mode); |
| 38 | + m_pGraphics->DrawImage(pImage, INT(start_point.x), INT(start_point.y), INT(size.cx), INT(size.cy)); |
28 | 39 | } |
29 | 40 |
|
30 | | -void CDrawCommonEx::SetFont(CFont * pFont) |
| 41 | +void CDrawCommonEx::SetBackColor(COLORREF back_color, BYTE alpha) |
| 42 | +{ |
| 43 | + m_back_color = CGdiPlusHelper::COLORREFToGdiplusColor(back_color, alpha); |
| 44 | +} |
| 45 | + |
| 46 | +void CDrawCommonEx::DrawWindowText(CRect rect, LPCTSTR lpszString, COLORREF color, Alignment align, bool draw_back_ground, bool multi_line, BYTE alpha) |
| 47 | +{ |
| 48 | + //设置字体 |
| 49 | + Gdiplus::Font font(m_pDC->GetSafeHdc()); |
| 50 | + //设置文本颜色 |
| 51 | + Gdiplus::SolidBrush brush(CGdiPlusHelper::COLORREFToGdiplusColor(color)); |
| 52 | + //设置对齐方式 |
| 53 | + Gdiplus::StringFormat format; |
| 54 | + Gdiplus::StringAlignment alignment = Gdiplus::StringAlignmentNear; |
| 55 | + if (align == Alignment::CENTER) |
| 56 | + alignment = Gdiplus::StringAlignmentCenter; |
| 57 | + else if (align == Alignment::RIGHT) |
| 58 | + alignment = Gdiplus::StringAlignmentFar; |
| 59 | + format.SetAlignment(alignment); //水平对齐方式 |
| 60 | + format.SetFormatFlags(Gdiplus::StringFormatFlagsNoFitBlackBox); |
| 61 | + format.SetLineAlignment(Gdiplus::StringAlignmentCenter); //垂直对齐方式 |
| 62 | + //矩形区域 |
| 63 | + Gdiplus::RectF rect_gdiplus = CGdiPlusHelper::CRectToGdiplusRect(rect); |
| 64 | + //绘制文本 |
| 65 | + m_pGraphics->DrawString(lpszString, -1, &font, rect_gdiplus, &format, &brush); |
| 66 | +} |
| 67 | + |
| 68 | +void CDrawCommonEx::SetDrawRect(CRect rect) |
| 69 | +{ |
| 70 | + m_pGraphics->SetClip(CGdiPlusHelper::CRectToGdiplusRect(rect)); |
| 71 | +} |
| 72 | + |
| 73 | +void CDrawCommonEx::FillRect(CRect rect, COLORREF color, BYTE alpha) |
| 74 | +{ |
| 75 | + Gdiplus::RectF rect_gdiplus = CGdiPlusHelper::CRectToGdiplusRect(rect); |
| 76 | + Gdiplus::SolidBrush brush(CGdiPlusHelper::COLORREFToGdiplusColor(color)); |
| 77 | + m_pGraphics->FillRectangle(&brush, rect_gdiplus); |
| 78 | +} |
| 79 | + |
| 80 | +void CDrawCommonEx::DrawRectOutLine(CRect rect, COLORREF color, int width, bool dot_line, BYTE alpha) |
| 81 | +{ |
| 82 | +} |
| 83 | + |
| 84 | +void CDrawCommonEx::DrawLine(CPoint start_point, int height, COLORREF color, BYTE alpha) |
| 85 | +{ |
| 86 | +} |
| 87 | + |
| 88 | +void CDrawCommonEx::SetTextColor(const COLORREF color, BYTE alpha) |
| 89 | +{ |
| 90 | + m_text_color = CGdiPlusHelper::COLORREFToGdiplusColor(color, alpha); |
| 91 | +} |
| 92 | + |
| 93 | +CDC* CDrawCommonEx::GetDC() |
| 94 | +{ |
| 95 | + return m_pDC; |
| 96 | +} |
| 97 | + |
| 98 | +int CDrawCommonEx::GetTextWidth(LPCTSTR lpszString) |
| 99 | +{ |
| 100 | + Gdiplus::Font font(m_pDC->GetSafeHdc()); |
| 101 | + Gdiplus::RectF textSize; |
| 102 | + m_pGraphics->MeasureString(lpszString, -1, &font, Gdiplus::PointF(0, 0), &textSize); |
| 103 | + return textSize.Width; |
| 104 | +} |
| 105 | + |
| 106 | +void CDrawCommonEx::DrawBitmap(HBITMAP hbitmap, CPoint start_point, CSize size, StretchMode stretch_mode, BYTE alpha) |
| 107 | +{ |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +///////////////////////////////////////////////////////////////////////////////////////////// |
| 112 | +///////////////////////////////////////////////////////////////////////////////////////////// |
| 113 | +Gdiplus::Color CGdiPlusHelper::COLORREFToGdiplusColor(COLORREF color, BYTE alpha /*= 255*/) |
| 114 | +{ |
| 115 | + return Gdiplus::Color(alpha, GetRValue(color), GetGValue(color), GetBValue(color)); |
| 116 | +} |
| 117 | + |
| 118 | +COLORREF CGdiPlusHelper::GdiplusColorToCOLORREF(Gdiplus::Color color) |
| 119 | +{ |
| 120 | + return RGB(color.GetR(), color.GetG(), color.GetB()); |
| 121 | +} |
| 122 | + |
| 123 | +CRect CGdiPlusHelper::GdiplusRectToCRect(Gdiplus::RectF rect) |
| 124 | +{ |
| 125 | + return CRect(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom()); |
| 126 | +} |
| 127 | + |
| 128 | +Gdiplus::RectF CGdiPlusHelper::CRectToGdiplusRect(CRect rect) |
31 | 129 | { |
32 | | - m_pFont = pFont; |
| 130 | + return Gdiplus::RectF(rect.left, rect.top, rect.Width(), rect.Height()); |
33 | 131 | } |
0 commit comments