XColorStatic.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "stdafx.h"
  2. #include "XColorStatic.h"
  3. #include "FontSize.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. /***************************************************************************************/
  10. // CXColorStatic
  11. BEGIN_MESSAGE_MAP(CXColorStatic, CStatic)
  12. // {{ AFX_MSG_MAP
  13. ON_WM_PAINT()
  14. ON_WM_ERASEBKGND()
  15. // }} AFX_MSG_MAP
  16. END_MESSAGE_MAP()
  17. /***************************************************************************************/
  18. // CXColorStatic
  19. CXColorStatic::CXColorStatic()
  20. {
  21. m_rgbText = GetSysColor(COLOR_BTNTEXT);
  22. m_rgbBackground = GetSysColor(COLOR_BTNFACE);
  23. m_pBrush = new CBrush(m_rgbBackground);
  24. m_bBold = FALSE;
  25. m_hIcon = NULL;
  26. m_iXMargin = 0;
  27. m_iYMargin = 0;
  28. }
  29. /***************************************************************************************/
  30. // ~CXColorStatic
  31. CXColorStatic::~CXColorStatic()
  32. {
  33. TRACE(_T("in CXColorStatic::!CXColorStatic\n"));
  34. if(m_font.GetSafeHandle())
  35. m_font.DeleteObject();
  36. if(m_pBrush)
  37. {
  38. m_pBrush->DeleteObject();
  39. delete m_pBrush;
  40. }
  41. m_pBrush = NULL;
  42. }
  43. /***************************************************************************************/
  44. // PreSubclassWindow
  45. void CXColorStatic::PreSubclassWindow()
  46. {
  47. TRACE(_T("in CXColorStatic::PreSubclassWindow\n"));
  48. // 获取字体
  49. CFont* pFont = GetFont();
  50. if(!pFont)
  51. {
  52. HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  53. if(hFont == NULL)
  54. hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
  55. if(hFont)
  56. pFont = CFont::FromHandle(hFont);
  57. }
  58. ASSERT(pFont);
  59. ASSERT(pFont->GetSafeHandle());
  60. // 为这个控件创建一个字体
  61. LOGFONT lf;
  62. pFont->GetLogFont(&lf);
  63. m_font.CreateFontIndirect(&lf);
  64. }
  65. /***************************************************************************************/
  66. // OnPaint
  67. void CXColorStatic::OnPaint()
  68. {
  69. CPaintDC dc(this); // 获取到绘制对象的设备上下文
  70. dc.SaveDC();
  71. dc.SetTextColor(m_rgbText);
  72. dc.SetBkColor(m_rgbBackground);
  73. dc.SetBkMode(OPAQUE);
  74. dc.SelectObject(m_pBrush);
  75. CRect rect;
  76. GetClientRect(rect);
  77. // 不能同时绘制图标和文本
  78. if(m_hIcon)
  79. {
  80. int iIconX = GetSystemMetrics(SM_CXICON);
  81. int iIconY = GetSystemMetrics(SM_CYICON);
  82. rect.left = rect.left + (rect.Width() - iIconX) / 2;
  83. rect.top = rect.top + (rect.Height() - iIconY) / 2;
  84. dc.DrawIcon(rect.left, rect.top, m_hIcon);
  85. }
  86. else
  87. {
  88. dc.SelectObject(&m_font);
  89. // 获取静态文本
  90. CString strText = _T("");
  91. GetWindowText(strText);
  92. UINT uiFormat = 0;
  93. DWORD dwStyle = GetStyle();
  94. // 重新绘制静态文本
  95. if(dwStyle && SS_CENTER)
  96. uiFormat |= DT_CENTER;
  97. else if(dwStyle && SS_LEFT)
  98. uiFormat |= DT_LEFT;
  99. else if(dwStyle && SS_RIGHT)
  100. uiFormat |= DT_RIGHT;
  101. if(dwStyle & SS_CENTERIMAGE) // 垂直居中 ==> 仅对单行
  102. uiFormat |= DT_VCENTER | DT_SINGLELINE;
  103. else
  104. uiFormat |= DT_WORDBREAK;
  105. uiFormat = DT_LEFT;
  106. rect.left += m_iXMargin;
  107. rect.top += m_iYMargin;
  108. dc.DrawText(strText, rect, uiFormat);
  109. }
  110. dc.RestoreDC(-1);
  111. }
  112. /***************************************************************************************/
  113. // OnEraseBkgnd
  114. BOOL CXColorStatic::OnEraseBkgnd(CDC *pDC)
  115. {
  116. CRect cr;
  117. GetClientRect(cr);
  118. pDC->FillRect(&cr, m_pBrush);
  119. return TRUE;
  120. }
  121. /***************************************************************************************/
  122. // SetFont
  123. void CXColorStatic::SetFont(LOGFONT *pLogFont, BOOL bRedraw /* = TRUE */)
  124. {
  125. ASSERT(pLogFont);
  126. if(!pLogFont)
  127. return;
  128. if(m_font.GetSafeHandle())
  129. m_font.DeleteObject();
  130. LOGFONT lf = *pLogFont;
  131. lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
  132. m_font.CreateFontIndirect(&lf);
  133. if(bRedraw)
  134. RedrawWindow();
  135. }
  136. /***************************************************************************************/
  137. // SetFont
  138. void CXColorStatic::SetFont(LPCTSTR lpszFaceName, int iPointSize, BOOL bRedraw /* = TRUE */)
  139. {
  140. // 如果指定的字体名字不存在或未指定, 将使用默认的字体
  141. LOGFONT lf;
  142. memset(&lf, 0, sizeof(lf));
  143. if(lpszFaceName == NULL)
  144. {
  145. CFont* pFont = GetFont();
  146. ASSERT(pFont);
  147. pFont->GetLogFont(&lf);
  148. }
  149. else
  150. {
  151. _tcsncpy_s(lf.lfFaceName, lpszFaceName, sizeof(lf.lfFaceName) / sizeof(TCHAR) - 1);
  152. }
  153. lf.lfHeight = GetFontHeight(iPointSize);
  154. SetFont(&lf, bRedraw);
  155. }
  156. /***************************************************************************************/
  157. // SetFont
  158. void CXColorStatic::SetFont(CFont *pFont, BOOL bRedraw /* = TRUE */)
  159. {
  160. ASSERT(pFont);
  161. if(!pFont)
  162. return;
  163. LOGFONT lf;
  164. memset(&lf, 0, sizeof(lf));
  165. pFont->GetLogFont(&lf);
  166. SetFont(&lf, bRedraw);
  167. }
  168. /***************************************************************************************/
  169. // SetTextColor
  170. void CXColorStatic::SetTextColor(COLORREF rgb, BOOL bRedraw /* = TRUE */)
  171. {
  172. m_rgbText = rgb;
  173. if(bRedraw)
  174. RedrawWindow();
  175. }
  176. /***************************************************************************************/
  177. // SetBold
  178. void CXColorStatic::SetBold(BOOL bFlag, BOOL bRedraw /* = TRUE */)
  179. {
  180. m_bBold = bFlag;
  181. LOGFONT lf;
  182. memset(&lf, 0, sizeof(lf));
  183. CFont* pFont = GetFont();
  184. ASSERT(pFont);
  185. pFont->GetLogFont(&lf);
  186. lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
  187. SetFont(&lf, bRedraw);
  188. }
  189. /***************************************************************************************/
  190. // SetBackgroundColor
  191. void CXColorStatic::SetBackgroundColor(COLORREF rgb, BOOL bRedraw /* = TRUE */)
  192. {
  193. m_rgbBackground = rgb;
  194. if(m_pBrush)
  195. {
  196. m_pBrush->DeleteObject();
  197. delete m_pBrush;
  198. }
  199. m_pBrush = new CBrush(m_rgbBackground);
  200. if(bRedraw)
  201. RedrawWindow();
  202. }
  203. /***************************************************************************************/
  204. // SetIcon
  205. void CXColorStatic::SetIcon(HICON hIcon, BOOL bRedraw /* = TRUE */)
  206. {
  207. ASSERT(hIcon);
  208. m_hIcon = hIcon;
  209. if(bRedraw)
  210. RedrawWindow();
  211. }