Label.cpp
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. // Label.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Label.h"
  5. #include "resource.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. clrBk =::GetSysColor(COLOR_3DFACE);
  16. clrText =::GetSysColor(COLOR_WINDOWTEXT);
  17. memset(&lgTextFont, 0, sizeof(LOGFONT));
  18. bFontBold =FALSE;
  19. bHyperLinkMode =FALSE;
  20. hCursor =::LoadCursor(::AfxGetInstanceHandle(), 
  21. MAKEINTRESOURCE(IDC_MYHAND));
  22. hOldCursor =NULL;
  23. }
  24. CLabel::~CLabel()
  25. {
  26. if(hCursor)
  27. {
  28. ::DestroyCursor(hCursor);
  29. }
  30. }
  31. BEGIN_MESSAGE_MAP(CLabel, CStatic)
  32. //{{AFX_MSG_MAP(CLabel)
  33. ON_WM_PAINT()
  34. ON_WM_SETCURSOR()
  35. ON_WM_LBUTTONDOWN()
  36. ON_WM_LBUTTONUP()
  37. ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
  38. ON_WM_MOUSEMOVE()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CLabel message handlers
  43. void CLabel::SetTextColor(COLORREF clr)
  44. {
  45. clrText =clr;
  46. }
  47. void CLabel::SetBkClolor(COLORREF clr)
  48. {
  49. clrBk =clr;
  50. }
  51. void CLabel::SetLabelFont(LPLOGFONT lpfnt)
  52. {
  53. ASSERT(lpfnt);
  54. if(NULL != lpfnt)
  55. {
  56. memcpy(&lgTextFont, lpfnt, sizeof(LOGFONT));
  57. }
  58. }
  59. void CLabel::SetFontBold(BOOL fontbold)
  60. {
  61. bFontBold =fontbold;
  62. }
  63. void CLabel::OnPaint() 
  64. {
  65. CPaintDC dc(this); // device context for painting
  66. HBITMAP hBmp =this->GetBitmap();
  67. if(hBmp)
  68. { //Draw Bmp
  69. CBitmap* pBmp =NULL;
  70. pBmp =pBmp->FromHandle(hBmp);
  71. if(pBmp && pBmp->GetSafeHandle())
  72. {
  73. BITMAP bmpsize;
  74. memset(&bmpsize, 0, sizeof(BITMAP));
  75. pBmp->GetBitmap(&bmpsize);
  76. CDC memdc;
  77. memdc.CreateCompatibleDC(&dc);
  78. if(memdc.GetSafeHdc())
  79. {
  80. CBitmap* pOldBmp =memdc.SelectObject(pBmp);
  81. dc.BitBlt(0, 0, bmpsize.bmWidth, bmpsize.bmHeight, 
  82. &memdc, 0, 0, SRCCOPY);
  83. memdc.SelectObject(pOldBmp);
  84. }
  85. memdc.DeleteDC();
  86. }
  87. return ;
  88. }
  89. RECT rect;
  90. this->GetClientRect(&rect);
  91. HBRUSH hBrush =::CreateSolidBrush(clrBk);
  92. ::FillRect(dc.GetSafeHdc(), &rect, hBrush);
  93. ::DeleteObject(hBrush);
  94. if(lgTextFont.lfWidth == 0 &&
  95. lgTextFont.lfHeight == 0) //字体没有由使用者设置
  96. { //使用默认字体
  97. CFont* pFont =this->GetFont();
  98. if(pFont)
  99. {
  100. pFont->GetLogFont(&lgTextFont);
  101. }
  102. }
  103. dc.SetTextColor(clrText);
  104. dc.SetBkMode(TRANSPARENT);
  105. if(IsFontBold())
  106. {
  107. lgTextFont.lfWeight =FW_BOLD;
  108. }
  109. if(IsHyperLinkMode())
  110. {
  111. lgTextFont.lfUnderline =TRUE;
  112. dc.SetTextColor(RGB(0, 0, 255));
  113. }
  114. else
  115. {
  116. lgTextFont.lfUnderline =FALSE;
  117. }
  118. HFONT hFont =::CreateFontIndirect(&lgTextFont);
  119. if(NULL == hFont)
  120. return;
  121. HFONT hOldFont=(HFONT)::SelectObject(dc.GetSafeHdc(), hFont);
  122. CString strText;
  123. this->GetWindowText(strText);
  124. DWORD dwStyle =this->GetStyle();
  125. if((dwStyle & SS_LEFT) == SS_LEFT)
  126. {
  127. dc.DrawText(strText, strText.GetLength(), &rect, DT_VCENTER | DT_SINGLELINE);
  128. }
  129. else
  130. if((dwStyle & SS_CENTER) == SS_CENTER)
  131. {
  132. dc.DrawText(strText, strText.GetLength(), 
  133. &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  134. }
  135. ::SelectObject(dc.GetSafeHdc(), hOldFont);
  136. ::DeleteObject(hFont);
  137. }
  138. BOOL CLabel::IsFontBold()
  139. {
  140. return bFontBold;
  141. }
  142. void CLabel::SetHyperLinkMode(BOOL bmode)
  143. {
  144. bHyperLinkMode =bmode;
  145. }
  146. BOOL CLabel::IsHyperLinkMode()
  147. {
  148. return bHyperLinkMode;
  149. }
  150. BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  151. {
  152. return CStatic::OnSetCursor(pWnd, nHitTest, message);
  153. }
  154. void CLabel::OnLButtonDown(UINT nFlags, CPoint point) 
  155. {
  156. CStatic::OnLButtonDown(nFlags, point);
  157. }
  158. void CLabel::OnLButtonUp(UINT nFlags, CPoint point) 
  159. {
  160. CStatic::OnLButtonUp(nFlags, point);
  161. }
  162. void CLabel::OnClicked() 
  163. {
  164. if(IsHyperLinkMode())
  165. {
  166. CWnd* pWnd =this->GetParent();
  167. if(pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
  168. {
  169. pWnd->SendMessage(WM_LABELCLK, 0, (LPARAM)::GetWindowLong(this->GetSafeHwnd(), 
  170. GWL_ID));
  171. }
  172. }
  173. }
  174. void CLabel::OnMouseMove(UINT nFlags, CPoint point) 
  175. {
  176. if(!IsHyperLinkMode())
  177. return;
  178. if(MK_LBUTTON == nFlags)
  179. {
  180. OnClicked(); 
  181. return;
  182. }
  183. if(FALSE == IsWindowEnabled())
  184. return ;
  185. POINT _point;
  186. ::GetCursorPos(&_point);
  187. RECT rect;
  188. this->GetWindowRect(&rect);
  189. if (::PtInRect( &rect, _point) &&
  190. ::WindowFromPoint(_point) == this ->GetSafeHwnd())
  191. { //鼠标移进了标签
  192. if(this->GetSafeHwnd() != ::GetCapture())
  193. {
  194. this->SetCapture();
  195. CWnd* pWnd =this->GetParent();
  196. if(NULL != pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
  197. {
  198. pWnd->PostMessage(WM_LABELMOVEIN, 0, 0);
  199. }
  200. if(hCursor)
  201. {
  202. hOldCursor =::SetCursor(hCursor);
  203. }
  204. }
  205. }
  206. else
  207. {
  208. if(this->GetSafeHwnd() == ::GetCapture())
  209. {
  210. //鼠标移出了标签
  211. ReleaseCapture();
  212. CWnd* pWnd =this->GetParent();
  213. if(NULL != pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
  214. {
  215. pWnd->PostMessage(WM_LABELMOVEOUT, 0, 0);
  216. }
  217. if(hOldCursor)
  218. {
  219. ::SetCursor(hOldCursor);
  220. }
  221. }
  222. }
  223. CStatic::OnMouseMove(nFlags, point);
  224. }