RTButton.cpp
上传用户:qhonly
上传日期:2013-06-10
资源大小:487k
文件大小:6k
源码类别:

界面编程

开发平台:

Visual C++

  1. // RTButton.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "RTButton.h"
  5. #include "RTDraw.h"
  6. // CRTButton
  7. IMPLEMENT_DYNAMIC(CRTButton, CButton)
  8. CRTButton::CRTButton()
  9. {
  10. m_ButtonState = StateNormal;
  11. m_MouseState = MouseStateNormal;
  12. }
  13. CRTButton::~CRTButton()
  14. {
  15. }
  16. CBitmap* CRTButton::m_ButtonBitmap[5] = {NULL,NULL,NULL,NULL,NULL};
  17. UINT     CRTButton::m_ButtonDrawMode[5] = {0,0,0,0,0};
  18. BOOL     CRTButton::m_IsEnableSkin = TRUE;
  19. BEGIN_MESSAGE_MAP(CRTButton, CButton)
  20. ON_WM_PAINT()
  21. ON_MESSAGE(WM_MOUSEMOVE,OnRTMouseMove)
  22. ON_MESSAGE(WM_LBUTTONDOWN,OnRTLButtonDown)
  23. ON_MESSAGE(WM_LBUTTONUP,OnRTLButtonUp)
  24. ON_MESSAGE(WM_LBUTTONDBLCLK,OnRTLButtonDBClick)
  25. END_MESSAGE_MAP()
  26. // CRTButton 消息处理程序
  27. void CRTButton::EnableSkin(BOOL IsEnable)
  28. {
  29. m_IsEnableSkin = IsEnable;
  30. }
  31. void CRTButton::OnPaint()
  32. {
  33. if(!m_IsEnableSkin)CButton::OnPaint();
  34. DWORD style = GetStyle();
  35. if((style & BS_PUSHBUTTON) != BS_PUSHBUTTON)
  36. return CButton::OnPaint();
  37. CPaintDC dc(this);
  38. RTDrawButton(&dc);
  39. }
  40. LRESULT CRTButton::OnRTLButtonDown(WPARAM wParam,LPARAM lParam)
  41. {
  42. if(!m_IsEnableSkin)return DefWindowProc(WM_LBUTTONDOWN,wParam,lParam);
  43. m_ButtonState = StateDown;
  44. m_MouseState = MouseStateDown;
  45. CClientDC dc(this);
  46. RTDrawButton(&dc);
  47. return 0;
  48. }
  49. LRESULT CRTButton::OnRTLButtonUp(WPARAM wParam,LPARAM lParam)
  50. {
  51. if(!m_IsEnableSkin)return DefWindowProc(WM_LBUTTONUP,wParam,lParam);
  52. ReleaseCapture();
  53. m_ButtonState = StateNormal;
  54. m_MouseState = MouseStateNormal;
  55. CClientDC dc(this);
  56. RTDrawButton(&dc);
  57. CRect rtButton;
  58. GetWindowRect(&rtButton);
  59. rtButton.SetRect(0,0,rtButton.Width(),rtButton.Height());
  60. CPoint point(lParam);
  61. if(rtButton.PtInRect(point))
  62. {
  63. CWnd *Parent = GetParent();
  64. if(Parent != NULL)Parent->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),BN_CLICKED),(LPARAM)m_hWnd);
  65. }
  66. return 0;
  67. }
  68. LRESULT CRTButton::OnRTMouseMove(WPARAM wParam,LPARAM lParam)
  69. {
  70. if(!m_IsEnableSkin)return DefWindowProc(WM_MOUSEMOVE,wParam,lParam);
  71. CRect rtButton;
  72. GetWindowRect(&rtButton);
  73. rtButton.SetRect(0,0,rtButton.Width(),rtButton.Height());
  74. CPoint point(lParam);
  75. if(wParam == MK_LBUTTON)
  76. {
  77. if(rtButton.PtInRect(point))
  78. {
  79. if(m_ButtonState != StateDown)
  80. {
  81. m_ButtonState = StateDown;
  82. CClientDC dc(this);
  83. RTDrawButton(&dc);
  84. }
  85. }
  86. else 
  87. {
  88. if(m_ButtonState == StateDown)
  89. {
  90. m_ButtonState = StateNormal;
  91. CClientDC dc(this);
  92. RTDrawButton(&dc);
  93. }
  94. }
  95. }
  96. else
  97. {
  98. if(rtButton.PtInRect(point))
  99. {
  100. if(m_ButtonState != StateFocus)
  101. {
  102. SetCapture();
  103. m_ButtonState = StateFocus;
  104. CClientDC dc(this);
  105. RTDrawButton(&dc);
  106. }
  107. }
  108. else 
  109. {
  110. if(m_ButtonState == StateFocus)
  111. {
  112. m_ButtonState = StateNormal;
  113. CClientDC dc(this);
  114. RTDrawButton(&dc);
  115. ReleaseCapture();
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. LRESULT CRTButton::OnRTLButtonDBClick(WPARAM wParam,LPARAM lParam)
  122. {
  123. if(!m_IsEnableSkin)return DefWindowProc(WM_MBUTTONDBLCLK,wParam,lParam);
  124. return 0;
  125. }
  126. void CRTButton::RTDrawButton(CDC *pDC)
  127. {
  128. CRect rtText;
  129. CRect rtWnd;
  130. GetClientRect(&rtText);
  131. CString ItemText;
  132. GetWindowText(ItemText);
  133. int OldBack = pDC->SetBkMode(TRANSPARENT);
  134. COLORREF OldText = pDC->SetTextColor(0xFFFF);
  135. CFont* pFont = GetFont();
  136. CFont* pOldFont = pDC->SelectObject(pFont);
  137. DWORD style = GetStyle();//GetButtonStyle();
  138. UINT uFormat = 0;
  139. if((style & BS_MULTILINE) != BS_MULTILINE)
  140. uFormat |= DT_SINGLELINE;
  141. if((style & BS_TOP) == BS_TOP)
  142. uFormat |= DT_TOP;
  143. else if((style & BS_BOTTOM) == BS_BOTTOM)
  144. uFormat |= DT_BOTTOM;
  145. else
  146. uFormat |= DT_VCENTER | DT_SINGLELINE;
  147. if((style & BS_LEFT) == BS_LEFT)
  148. uFormat |= DT_LEFT;
  149. else if((style & BS_RIGHT) == BS_RIGHT)
  150. uFormat |= DT_RIGHT;
  151. else
  152. uFormat |= DT_CENTER;
  153. uFormat |= DT_END_ELLIPSIS;
  154. if(style & WS_DISABLED)
  155. {
  156. m_ButtonState = StateDisable;
  157. if(!CRTDraw::RTDrawBitmap(pDC,&rtText,m_ButtonBitmap[BMP_DISABLE],m_ButtonDrawMode[BMP_DISABLE]))
  158. {
  159. pDC->DrawEdge(&rtText,BDR_RAISEDINNER,BF_RECT);
  160. }
  161. COLORREF ColorWhite = GetSysColor(COLOR_BTNTEXT);
  162. COLORREF ColorGray = GetSysColor(COLOR_BTNSHADOW);
  163. pDC->SetTextColor(ColorWhite);
  164. pDC->DrawText(ItemText,&rtText,uFormat);
  165. rtText.top += 1;
  166. rtText.left += 1;
  167. pDC->SetTextColor(ColorGray);
  168. pDC->DrawText(ItemText,&rtText,uFormat);
  169. return;
  170. }
  171. if(m_ButtonState == StateNormal)
  172. {
  173. if(!CRTDraw::RTDrawBitmap(pDC,&rtText,m_ButtonBitmap[BMP_NORMAL],m_ButtonDrawMode[BMP_NORMAL]))
  174. {
  175. pDC->DrawEdge(&rtText,BDR_RAISEDINNER,BF_RECT);
  176. }
  177. }
  178. else if(m_ButtonState == StateFocus)
  179. {
  180. if(!CRTDraw::RTDrawBitmap(pDC,&rtText,m_ButtonBitmap[BMP_FOCUS],m_ButtonDrawMode[BMP_FOCUS]))
  181. {
  182. pDC->DrawEdge(&rtText,BDR_RAISEDINNER,BF_RECT);
  183. }
  184. }
  185. else
  186. {
  187. if(!CRTDraw::RTDrawBitmap(pDC,&rtText,m_ButtonBitmap[BMP_DOWN],m_ButtonDrawMode[BMP_DOWN]))
  188. {
  189. pDC->DrawEdge(&rtText,BDR_SUNKENOUTER,BF_RECT);
  190. }
  191. }
  192. COLORREF ColorWhite = GetSysColor(COLOR_BTNTEXT);
  193. pDC->SetTextColor(ColorWhite);
  194. pDC->DrawText(ItemText,&rtText,uFormat);
  195. pDC->SelectObject(pOldFont);
  196. pDC->SetBkMode(OldBack);
  197. pDC->SetTextColor(OldText);
  198. }
  199. void CRTButton::SetButtonBitmap(CBitmap* ButtonBitmap[],UINT DrawMode[])
  200. {
  201. for(int i = 0; i < 5; i ++)
  202. {
  203. m_ButtonBitmap[i] = ButtonBitmap[i];
  204. m_ButtonDrawMode[i] = DrawMode[i];
  205. }
  206. }
  207. LRESULT CRTButton::OnRTMouseLeave(WPARAM wParam,LPARAM lParam)
  208. {
  209. if(!m_IsEnableSkin)return DefWindowProc(WM_MOUSELEAVE,wParam,lParam);
  210. return 0;
  211. }
  212. LRESULT CRTButton::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  213. {
  214. return CButton::WindowProc(message, wParam, lParam);
  215. }