Label.cpp
上传用户:lxjxxg
上传日期:2007-01-02
资源大小:42k
文件大小:5k
源码类别:

工具条

开发平台:

Visual C++

  1. // Label.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Resource.h"
  5. #include "Label.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CLabel
  13. CLabel::CLabel()
  14. {
  15. m_crText = GetSysColor(COLOR_WINDOWTEXT);
  16. m_hBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  17. ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
  18. m_font.CreateFontIndirect(&m_lf);
  19. m_bTimer = FALSE;
  20. m_bState = FALSE;
  21. m_bLink = TRUE;
  22. m_hCursor = NULL;
  23. m_Type = None;
  24. m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  25. strURL = _T("") ;
  26. }
  27. CLabel::~CLabel()
  28. {
  29. m_font.DeleteObject();
  30. ::DeleteObject(m_hBrush);
  31. }
  32. CLabel& CLabel::SetText(const CString& strText)
  33. {
  34. SetWindowText(strText);
  35. return *this;
  36. }
  37. CLabel& CLabel::SetTextColor(COLORREF crText)
  38. {
  39. m_crText = crText;
  40. RedrawWindow();
  41. return *this;
  42. }
  43. CLabel& CLabel::SetFontBold(BOOL bBold)
  44. {
  45. m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
  46. ReconstructFont();
  47. RedrawWindow();
  48. return *this;
  49. }
  50. CLabel& CLabel::SetFontUnderline(BOOL bSet)
  51. {
  52. m_lf.lfUnderline = bSet;
  53. ReconstructFont();
  54. RedrawWindow();
  55. return *this;
  56. }
  57. CLabel& CLabel::SetFontItalic(BOOL bSet)
  58. {
  59. m_lf.lfItalic = bSet;
  60. ReconstructFont();
  61. RedrawWindow();
  62. return *this;
  63. }
  64. CLabel& CLabel::SetSunken(BOOL bSet)
  65. {
  66. if (!bSet)
  67. ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
  68. else
  69. ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
  70. return *this;
  71. }
  72. CLabel& CLabel::SetBorder(BOOL bSet)
  73. {
  74. if (!bSet)
  75. ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
  76. else
  77. ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
  78. return *this;
  79. }
  80. CLabel& CLabel::SetFontSize(int nSize)
  81. {
  82. nSize*=-1;
  83. m_lf.lfHeight = nSize;
  84. ReconstructFont();
  85. RedrawWindow();
  86. return *this;
  87. }
  88. CLabel& CLabel::SetBkColor(COLORREF crBkgnd)
  89. {
  90. if (m_hBrush)
  91. ::DeleteObject(m_hBrush);
  92. m_hBrush = ::CreateSolidBrush(crBkgnd);
  93. return *this;
  94. }
  95. CLabel& CLabel::SetFontName(const CString& strFont)
  96. {
  97. strcpy(m_lf.lfFaceName,strFont);
  98. ReconstructFont();
  99. RedrawWindow();
  100. return *this;
  101. }
  102. BEGIN_MESSAGE_MAP(CLabel, CStatic)
  103. //{{AFX_MSG_MAP(CLabel)
  104. ON_WM_CTLCOLOR_REFLECT()
  105. ON_WM_TIMER()
  106. ON_WM_LBUTTONDOWN()
  107. ON_WM_SETCURSOR()
  108. //}}AFX_MSG_MAP
  109. END_MESSAGE_MAP()
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CLabel message handlers
  112. HBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor) 
  113. {
  114. // TODO: Change any attributes of the DC here
  115. // TODO: Return a non-NULL brush if the parent's handler should not be called
  116. if (CTLCOLOR_STATIC == nCtlColor)
  117. {
  118. pDC->SelectObject(&m_font);
  119. pDC->SetTextColor(m_crText);
  120. pDC->SetBkMode(TRANSPARENT);
  121. }
  122. if (m_Type == Background)
  123. {
  124. if (!m_bState)
  125. return m_hwndBrush;
  126. }
  127. return m_hBrush;
  128. }
  129. void CLabel::ReconstructFont()
  130. {
  131. m_font.DeleteObject();
  132. BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
  133. ASSERT(bCreated);
  134. }
  135. CLabel& CLabel::FlashText(BOOL bActivate)
  136. {
  137. if (m_bTimer)
  138. {
  139. SetWindowText(m_strText);
  140. KillTimer(1);
  141. }
  142. if (bActivate)
  143. {
  144. GetWindowText(m_strText);
  145. m_bState = FALSE;
  146. m_bTimer = TRUE;
  147. SetTimer(1,500,NULL);
  148. m_Type = Text;
  149. }
  150. return *this;
  151. }
  152. CLabel& CLabel::FlashBackground(BOOL bActivate)
  153. {
  154. if (m_bTimer)
  155. KillTimer(1);
  156. if (bActivate)
  157. {
  158. m_bState = FALSE;
  159. m_bTimer = TRUE;
  160. SetTimer(1,500,NULL);
  161. m_Type = Background;
  162. }
  163. return *this;
  164. }
  165. void CLabel::OnTimer(UINT nIDEvent) 
  166. {
  167. m_bState = !m_bState;
  168. switch (m_Type)
  169. {
  170. case Text:
  171. if (m_bState)
  172. SetWindowText("");
  173. else
  174. SetWindowText(m_strText);
  175. break;
  176. case Background:
  177. InvalidateRect(NULL,FALSE);
  178. UpdateWindow();
  179. break;
  180. }
  181. CStatic::OnTimer(nIDEvent);
  182. }
  183. CLabel& CLabel::SetLink(BOOL bLink)
  184. {
  185. m_bLink = bLink;
  186. if (bLink)
  187. ModifyStyle(0,SS_NOTIFY);
  188. else
  189. ModifyStyle(SS_NOTIFY,0);
  190. return *this;
  191. }
  192. void CLabel::OnLButtonDown(UINT nFlags, CPoint point) 
  193. {
  194. // CString strLink;
  195. // GetWindowText(strLink);
  196. if(strURL.IsEmpty())
  197. {
  198. TRACE0("Use must set url for the link by calling SetLinkURL()");
  199. return ;
  200. }
  201. ShellExecute(NULL,"open",strURL,NULL,NULL,SW_SHOWNORMAL);
  202. CStatic::OnLButtonDown(nFlags, point);
  203. }
  204. BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  205. {
  206. if (m_hCursor)
  207. {
  208. ::SetCursor(m_hCursor);
  209. return TRUE;
  210. }
  211. return CStatic::OnSetCursor(pWnd, nHitTest, message);
  212. }
  213. CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
  214. {
  215. m_hCursor = hCursor;
  216. return *this;
  217. }
  218. void CLabel::SetLinkURL(const char * szURL)
  219. {
  220. strURL = szURL;
  221. }