xref: /MusicPlayer2/MusicPlayer2/StaticEx.cpp (revision bdab36f8b39bc8fbd10db7970e6ea1bfffbbdfed)
1 #include "stdafx.h"
2 #include "StaticEx.h"
3 #include "DrawCommon.h"
4 #include "MusicPlayer2.h"
5 
6 
CStaticEx()7 CStaticEx::CStaticEx()
8 {
9 }
10 
11 
~CStaticEx()12 CStaticEx::~CStaticEx()
13 {
14 }
15 
SetTextColor(COLORREF text_color)16 void CStaticEx::SetTextColor(COLORREF text_color)
17 {
18 	m_text_color = text_color;
19 }
20 
SetBackgroundColor(COLORREF back_color)21 void CStaticEx::SetBackgroundColor(COLORREF back_color)
22 {
23 	m_back_color = back_color;
24 	m_transparent = false;
25 }
26 
SetWindowText(LPCTSTR lpszString)27 void CStaticEx::SetWindowText(LPCTSTR lpszString)
28 {
29     CWnd::SetWindowText(lpszString);
30     Invalidate();
31 }
32 
GetWindowText() const33 CString CStaticEx::GetWindowText() const
34 {
35     CString str;
36     CStatic::GetWindowText(str);
37     return str;
38 }
39 
SetIcon(IconMgr::IconType icon_type)40 void CStaticEx::SetIcon(IconMgr::IconType icon_type)
41 {
42     m_icon_type = icon_type;
43 }
44 
BEGIN_MESSAGE_MAP(CStaticEx,CStatic)45 BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
46 	ON_WM_PAINT()
47 END_MESSAGE_MAP()
48 
49 
50 void CStaticEx::OnPaint()
51 {
52 	CPaintDC dc(this); // device context for painting
53 					   // TODO: 在此处添加消息处理程序代码
54 					   // 不为绘图消息调用 CStatic::OnPaint()
55 
56 	CDrawCommon draw;
57 	draw.Create(&dc, GetFont());
58 
59 	CRect rect;
60 	GetClientRect(rect);
61 	CString str;
62 	CStatic::GetWindowText(str);
63 	//绘制背景
64 	if(!m_transparent)
65 		draw.FillRect(rect, m_back_color);
66 	else
67 		DrawThemeParentBackground(m_hWnd, dc.GetSafeHdc(), &rect);	//重绘控件区域以解决文字重叠的问题
68 
69 	CRect rc_text = rect;
70 
71 	//绘制图标
72     if (m_icon_type != IconMgr::IconType::IT_NO_ICON)
73 	{
74         HICON hIcon = theApp.m_icon_mgr.GetHICON(m_icon_type, IconMgr::IconStyle::IS_OutlinedDark, IconMgr::IconSize::IS_DPI_16);
75         CSize icon_size = IconMgr::GetIconSize(IconMgr::IconSize::IS_DPI_16);
76 		CRect rc_tmp = rect;
77         rc_tmp.right = rc_tmp.left + icon_size.cx + theApp.DPI(4);
78 		rc_text.left = rc_tmp.right;
79 		CPoint icon_left_top;
80         icon_left_top.x = rc_tmp.left + (rc_tmp.Width() - icon_size.cx) / 2;
81         icon_left_top.y = rc_tmp.top + (rc_tmp.Height() - icon_size.cy) / 2;
82         draw.DrawIcon(hIcon, icon_left_top, icon_size);
83 	}
84 
85 	//绘制文本
86 	draw.DrawWindowText(rc_text, str, m_text_color);
87 }
88