Label.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:5k
源码类别:

VC书籍

开发平台:

Visual C++

  1. // Label.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "robot.h"
  5. #include "Label.h"
  6. //#include "shellapi.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CLabel
  14. CLabel::CLabel()
  15. {
  16. m_crText = GetSysColor(COLOR_WINDOWTEXT);
  17. m_hBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  18. ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
  19. m_font.CreateFontIndirect(&m_lf);
  20. m_bTimer = FALSE;
  21. m_bState = FALSE;
  22. m_bLink = TRUE;
  23. m_hCursor = NULL;
  24. m_Type = None;
  25. m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
  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. ShellExecute(NULL,"open",strLink,NULL,NULL,SW_SHOWNORMAL);
  197. CStatic::OnLButtonDown(nFlags, point);
  198. }
  199. BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  200. {
  201. if (m_hCursor)
  202. {
  203. ::SetCursor(m_hCursor);
  204. return TRUE;
  205. }
  206. return CStatic::OnSetCursor(pWnd, nHitTest, message);
  207. }
  208. CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
  209. {
  210. m_hCursor = hCursor;
  211. return *this;
  212. }