SkinButton.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SkinButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. //#include "dialog.h"
  5. #include "SkinButton.h"
  6. #include "EnBitmap.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. extern CToolTipCtrl g_ToolTip;
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CSkinButton
  15. CSkinButton::CSkinButton()
  16. {
  17. m_bMouseIn = m_bDown = FALSE;
  18. m_nStyle=STYLE_BITMAP;
  19. }
  20. CSkinButton::~CSkinButton()
  21. {
  22. }
  23. BEGIN_MESSAGE_MAP(CSkinButton, CButton)
  24. //{{AFX_MSG_MAP(CSkinButton)
  25. ON_WM_PAINT()
  26. ON_WM_LBUTTONDOWN()
  27. ON_WM_LBUTTONUP()
  28. ON_WM_ERASEBKGND()
  29. ON_WM_MOUSEMOVE()
  30. ON_WM_LBUTTONDBLCLK()
  31. ON_WM_SETFOCUS()
  32. ON_WM_KILLFOCUS()
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CSkinButton message handlers
  37. void CSkinButton::OnPaint()
  38. {
  39. CPaintDC dc(this); // device context for painting
  40. m_bFocus = GetFocus()->GetSafeHwnd() == m_hWnd;
  41. m_bEnable = !(GetWindowLong(m_hWnd,GWL_STYLE) & WS_DISABLED);
  42. CEnBitmap bmp;
  43. if (m_nStyle & STYLE_BITMAP)
  44. {
  45. if ( !m_bEnable )
  46. {
  47. //disable
  48. m_bmpButton.DrawImage(bmp,4,1,4,1);
  49. }
  50. else
  51. if ( m_bMouseIn &&m_bDown )
  52. {
  53. //down
  54. m_bmpButton.DrawImage(bmp,3,1,4,1);
  55. }
  56. else if ( m_bMouseIn )
  57. {
  58. //hover
  59. m_bmpButton.DrawImage(bmp,2,1,4,1);
  60. }
  61. else if ( m_bFocus )
  62. {
  63. //normal with focus
  64. m_bmpButton.DrawImage(bmp,1,1,4,1);
  65. }
  66. else
  67. {
  68. //normal
  69. m_bmpButton.DrawImage(bmp,1,1,4,1);
  70. }
  71. //设置区域
  72. SetWindowRgn(bmp.BitmapToRegion(RGB(255,0,255)), TRUE );
  73. HRGN hRgn = CreateRectRgn( 0, 0, 0, 0);
  74. GetWindowRgn(hRgn);
  75. //选入位图绘画区域
  76. ::SelectClipRgn(dc.GetSafeHdc(), hRgn);
  77. bmp.Draw( &dc, CRect(0,0,bmp.GetWidth(),bmp.GetHeight()));
  78. dc.SelectClipRgn(NULL);
  79. DeleteObject(hRgn);
  80. DrawText( &dc ,bmp.GetRect() ,RGB(0,0,0));
  81. }
  82. else if (m_nStyle & STYLE_TOOLBAR)
  83. {
  84. CRect rcButton;
  85. GetClientRect(&rcButton);
  86. CRect rcImage,rcText;
  87. rcImage=m_bmpButton.GetRect();
  88. int nMaigin=(rcButton.Height()- rcImage.Height())/2;
  89. rcImage.OffsetRect(nMaigin,nMaigin);
  90. rcText=rcButton;
  91. rcText.left =rcImage.right ;
  92. //先更新背景
  93. CRect rc=rcButton;
  94. ClientToScreen(&rcButton);
  95. GetParent()->ScreenToClient(&rcButton);
  96. GetParent()->RedrawWindow(&rcButton,NULL,RDW_ERASE|RDW_UPDATENOW |RDW_INVALIDATE|RDW_NOCHILDREN );
  97. rc.DeflateRect(1,1);
  98. if ( !m_bEnable )
  99. {
  100. //disable
  101. }
  102. else
  103. if ( m_bMouseIn &&m_bDown )
  104. {
  105. //down
  106. dc.Draw3dRect(&rc,GetSysColor(COLOR_3DDKSHADOW),GetSysColor(COLOR_3DHILIGHT));
  107. }
  108. else if ( m_bMouseIn )
  109. {
  110. dc.Draw3dRect(&rc,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DDKSHADOW));
  111. }
  112. else if ( m_bFocus )
  113. {
  114. }
  115. else
  116. {
  117. }
  118. m_bmpButton.TransparentBlt(dc, &rcImage,RGB(255,0,255));
  119. DrawText( &dc ,rcText,m_crText);
  120. }
  121. }
  122. void CSkinButton::LoadBitmap(LPCTSTR szImagePath)
  123. {
  124. m_bmpButton.LoadImage(szImagePath);
  125. }
  126. BOOL CSkinButton::LoadBitmap(UINT uIDRes,COLORREF crBack)
  127. {
  128. return m_bmpButton.LoadBitmap(uIDRes);
  129. }
  130. void CSkinButton::OnLButtonDown(UINT nFlags, CPoint point)
  131. {
  132. m_bDown = TRUE;
  133. m_bMouseIn = TRUE;
  134. Invalidate();
  135. UpdateWindow();
  136. SetFocus();
  137. }
  138. void CSkinButton::OnLButtonUp(UINT nFlags, CPoint point)
  139. {
  140. // TODO: Add your message handler code here and/or call default
  141. ReleaseCapture();
  142. m_bDown = FALSE;
  143. if ( m_bMouseIn )
  144. {
  145. m_bMouseIn = FALSE;
  146. //GetRegion();
  147. Invalidate();
  148. UpdateWindow();
  149. GetParent()->SendMessage( WM_COMMAND, GetDlgCtrlID(), (LPARAM)m_hWnd );
  150. }
  151. else
  152. {
  153. //GetRegion();
  154. Invalidate();
  155. UpdateWindow();
  156. }
  157. }
  158. BOOL CSkinButton::OnEraseBkgnd(CDC* pDC)
  159. {
  160. return TRUE;
  161. }
  162. void CSkinButton::OnMouseMove(UINT nFlags, CPoint point)
  163. {
  164. // TODO: Add your message handler code here and/or call default
  165. if (m_nStyle & STYLE_BITMAP)
  166. {
  167. HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
  168. GetWindowRgn(hRgn);
  169. if ( PtInRegion( hRgn, point.x, point.y ))
  170. {
  171. ::SetCursor(AfxGetApp()->LoadCursor(IDC_HANDCUR ));
  172. if ( !m_bMouseIn)
  173. {
  174. m_bMouseIn = TRUE;
  175. Invalidate();
  176. UpdateWindow();
  177. SetCapture();
  178. }
  179. }
  180. else
  181. {
  182. if ( m_bMouseIn)
  183. {
  184. m_bMouseIn = FALSE;
  185. //GetRegion();
  186. Invalidate();
  187. UpdateWindow();
  188. if ( !m_bDown )
  189. ReleaseCapture();
  190. }
  191. }
  192. DeleteObject(hRgn);
  193. }
  194. else if (m_nStyle & STYLE_TOOLBAR)
  195. {
  196. CRect rc;
  197. GetClientRect(&rc);
  198. if ( rc.PtInRect( point ))
  199. {
  200. //移入
  201. if ( !m_bMouseIn) {
  202. m_bMouseIn = TRUE;
  203. Invalidate();
  204. UpdateWindow();
  205. SetCapture();
  206. }
  207. }
  208. else
  209. {
  210. //移出
  211. if ( m_bMouseIn) {
  212. m_bMouseIn = FALSE;
  213. //GetRegion();
  214. Invalidate();
  215. UpdateWindow();
  216. if ( !m_bDown )
  217. ReleaseCapture();
  218. }
  219. }
  220. }
  221. }
  222. void CSkinButton::OnLButtonDblClk(UINT nFlags, CPoint point)
  223. {
  224. }
  225. void CSkinButton::OnSetFocus(CWnd* pOldWnd)
  226. {
  227. }
  228. void CSkinButton::OnKillFocus(CWnd* pNewWnd)
  229. {
  230. }
  231. BOOL CSkinButton::DrawText(CDC *pDC,CRect rc ,COLORREF crText)
  232. {
  233. CRect r;
  234. CString str;
  235. CRect rcButton;
  236. CString strText;
  237. UINT nId=GetDlgCtrlID( ) ;
  238. if (strText.LoadString(nId)) {
  239. CFont *ofont;
  240. ofont = pDC->SelectObject( GetParent()->GetFont() );
  241. pDC->SetTextColor(crText);
  242. pDC->SetBkMode(TRANSPARENT);
  243. pDC->DrawText( strText, rc, DT_CENTER | DT_SINGLELINE | DT_VCENTER );
  244. pDC->SelectObject(ofont);
  245. }
  246. return TRUE;
  247. }
  248. HBITMAP CSkinButton::SetBitmap(HBITMAP hBitmap)
  249. {
  250. return m_bmpButton.SetBitmap(hBitmap) ;
  251. }
  252. void CSkinButton::SetTextColor(COLORREF cr)
  253. {
  254. m_crText=cr;
  255. }