LABEL.CPP 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**************************************************************************************************
  2. 版本所有(C), 2020-2022, 北京普达迪泰
  3. ***************************************************************************************************
  4. 文 件 名: Label.cpp
  5. 功能描述: 自定义UI上的文本标签
  6. 版 本 号: 1.00初版
  7. 作 者: HYF
  8. 生成日期; 2021-12-20
  9. 最近修改:
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 :2021-12-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #include "pch.h"
  17. #include "Resource.h"
  18. #include "Label.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CLabel
  26. CLabel::CLabel()
  27. {
  28. m_crText = RGB(112,112, 112);
  29. m_hBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  30. ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
  31. strcpy_s(m_lf.lfFaceName, _T("微软雅黑"));
  32. m_font.CreateFontIndirect(&m_lf);
  33. m_bTimer = FALSE;
  34. m_bState = FALSE;
  35. m_bLink = TRUE;
  36. m_hCursor = NULL;
  37. m_Type = None;
  38. m_bTrans = TRUE;
  39. m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  40. }
  41. CLabel::~CLabel()
  42. {
  43. m_font.DeleteObject();
  44. ::DeleteObject(m_hBrush);
  45. }
  46. CLabel& CLabel::SetText(const CString& strText)
  47. {
  48. RedrawWindow();
  49. SetWindowText(strText);
  50. return *this;
  51. }
  52. CLabel& CLabel::SetTextColor(COLORREF crText)
  53. {
  54. m_crText = crText;
  55. RedrawWindow();
  56. return *this;
  57. }
  58. CLabel& CLabel::SetFontBold(BOOL bBold)
  59. {
  60. m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
  61. ReconstructFont();
  62. RedrawWindow();
  63. return *this;
  64. }
  65. CLabel& CLabel::SetFontUnderline(BOOL bSet)
  66. {
  67. m_lf.lfUnderline = bSet;
  68. ReconstructFont();
  69. RedrawWindow();
  70. return *this;
  71. }
  72. CLabel& CLabel::SetFontItalic(BOOL bSet)
  73. {
  74. m_lf.lfItalic = bSet;
  75. ReconstructFont();
  76. RedrawWindow();
  77. return *this;
  78. }
  79. CLabel& CLabel::SetSunken(BOOL bSet)
  80. {
  81. if (!bSet)
  82. ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
  83. else
  84. ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
  85. return *this;
  86. }
  87. CLabel& CLabel::SetBorder(BOOL bSet)
  88. {
  89. if (!bSet)
  90. ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
  91. else
  92. ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
  93. return *this;
  94. }
  95. CLabel& CLabel::SetFontSize(int nSize)
  96. {
  97. nSize*=-1;
  98. m_lf.lfHeight = nSize;
  99. ReconstructFont();
  100. RedrawWindow();
  101. return *this;
  102. }
  103. CLabel& CLabel::SetTransparent(BOOL bTrans)
  104. {
  105. m_bTrans = bTrans;
  106. RedrawWindow();
  107. return *this;
  108. }
  109. CLabel& CLabel::SetBkColor(COLORREF crBkgnd)
  110. {
  111. if (m_hBrush)
  112. ::DeleteObject(m_hBrush);
  113. m_hBrush = ::CreateSolidBrush(crBkgnd);
  114. return *this;
  115. }
  116. CLabel& CLabel::SetFontName(const CString& strFont)
  117. {
  118. strcpy_s(m_lf.lfFaceName,strFont);
  119. ReconstructFont();
  120. RedrawWindow();
  121. return *this;
  122. }
  123. BEGIN_MESSAGE_MAP(CLabel, CStatic)
  124. //{{AFX_MSG_MAP(CLabel)
  125. ON_WM_CTLCOLOR_REFLECT()
  126. ON_WM_TIMER()
  127. ON_WM_LBUTTONDOWN()
  128. ON_WM_SETCURSOR()
  129. //}}AFX_MSG_MAP
  130. END_MESSAGE_MAP()
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CLabel message handlers
  133. HBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor)
  134. {
  135. if (CTLCOLOR_STATIC == nCtlColor)
  136. {
  137. pDC->SelectObject(&m_font);
  138. pDC->SetTextColor(m_crText);
  139. if (m_bTrans)
  140. pDC->SetBkMode(TRANSPARENT);
  141. }
  142. if (m_Type == Background)
  143. {
  144. if (!m_bState)
  145. return m_hwndBrush;
  146. }
  147. return (HBRUSH)::GetStockObject(NULL_BRUSH);
  148. }
  149. void CLabel::ReconstructFont()
  150. {
  151. m_font.DeleteObject();
  152. BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
  153. ASSERT(bCreated);
  154. }
  155. CLabel& CLabel::FlashText(BOOL bActivate)
  156. {
  157. if (m_bTimer)
  158. {
  159. SetWindowText(m_strText);
  160. KillTimer(1);
  161. }
  162. if (bActivate)
  163. {
  164. GetWindowText(m_strText);
  165. m_bState = FALSE;
  166. m_bTimer = TRUE;
  167. SetTimer(1,500,NULL);
  168. m_Type = Text;
  169. }
  170. return *this;
  171. }
  172. CLabel& CLabel::FlashBackground(BOOL bActivate)
  173. {
  174. if (m_bTimer)
  175. KillTimer(1);
  176. if (bActivate)
  177. {
  178. m_bState = FALSE;
  179. m_bTimer = TRUE;
  180. SetTimer(1,500,NULL);
  181. m_Type = Background;
  182. }
  183. return *this;
  184. }
  185. void CLabel::OnTimer(UINT_PTR nIDEvent)
  186. {
  187. m_bState = !m_bState;
  188. switch (m_Type)
  189. {
  190. case Text:
  191. if (m_bState)
  192. SetWindowText(_T(""));
  193. else
  194. SetWindowText(m_strText);
  195. break;
  196. case Background:
  197. InvalidateRect(NULL,FALSE);
  198. UpdateWindow();
  199. break;
  200. }
  201. CStatic::OnTimer(nIDEvent);
  202. }
  203. CLabel& CLabel::SetLink(BOOL bLink)
  204. {
  205. m_bLink = bLink;
  206. if (bLink)
  207. ModifyStyle(0,SS_NOTIFY);
  208. else
  209. ModifyStyle(SS_NOTIFY,0);
  210. return *this;
  211. }
  212. void CLabel::OnLButtonDown(UINT nFlags, CPoint point)
  213. {
  214. CString strLink;
  215. GetWindowText(strLink);
  216. ShellExecute(NULL, _T("open"),strLink,NULL,NULL,SW_SHOWNORMAL);
  217. CStatic::OnLButtonDown(nFlags, point);
  218. }
  219. BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  220. {
  221. if (m_hCursor)
  222. {
  223. ::SetCursor(m_hCursor);
  224. return TRUE;
  225. }
  226. return CStatic::OnSetCursor(pWnd, nHitTest, message);
  227. }
  228. CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
  229. {
  230. m_hCursor = hCursor;
  231. return *this;
  232. }