CoolBtn.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:5k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: CoolBtn.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "WndX.h"
  13. #include "CoolBtn.h"
  14. #include <Windowsx.h>
  15. /*------------------------------------------------------------------------------*/
  16. CCoolBtn::~CCoolBtn()
  17. {
  18. }
  19. /*------------------------------------------------------------------------------*/
  20. CCoolBtn::CCoolBtn()
  21. {
  22. m_hNormal=0;
  23. m_hHover=0;
  24. m_hDown=0;
  25. m_uState=0;
  26. }
  27. /*------------------------------------------------------------------------------*/
  28. LRESULT CCoolBtn::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  29. {
  30. switch(uMsg)
  31. {
  32. case WM_PAINT:
  33. OnPaint();
  34. return TRUE;
  35. case WM_ERASEBKGND:
  36. return TRUE;
  37. case WM_DESTROY:
  38. {
  39. //销毁载入的资源
  40. if(m_hNormal)
  41. DeleteObject(m_hNormal);
  42. if(m_hHover)
  43. DeleteObject(m_hHover);
  44. if(m_hDown)
  45. DeleteObject(m_hDown);
  46. m_hNormal=0;
  47. m_hHover=0;
  48. m_hDown=0;
  49. }
  50. break;
  51. case WM_MOUSEMOVE:
  52. {
  53. POINT point;
  54. point.x=GET_X_LPARAM(lParam); 
  55. point.y=GET_Y_LPARAM(lParam); 
  56. OnMouseMove(wParam,point);
  57. }
  58. return TRUE;
  59. case WM_LBUTTONDBLCLK:
  60. case WM_LBUTTONDOWN:
  61. {
  62. POINT point;
  63. point.x=GET_X_LPARAM(lParam); 
  64. point.y=GET_Y_LPARAM(lParam); 
  65. OnLButtonDown(wParam,point);
  66. }
  67. return TRUE;
  68. case WM_TIMER:
  69. OnTimer(wParam);
  70. break;
  71. }
  72. return CallWindowProc(m_OldProc,m_hWnd,uMsg,wParam,lParam);
  73. }
  74. /*------------------------------------------------------------------------------*/
  75. void CCoolBtn::OnPaint()
  76. {
  77. //画三种状态时的按钮
  78. HBITMAP h;
  79. switch(m_uState)
  80. {
  81. case 0:
  82. h=m_hNormal;
  83. break;
  84. case 1:
  85. h=m_hHover;
  86. break;
  87. case 2:
  88. h=m_hDown;
  89. break;
  90. }
  91. PAINTSTRUCT ps;
  92. HDC hdc=BeginPaint(m_hWnd,&ps);
  93. HDC hMemDC=CreateCompatibleDC(hdc);
  94. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,h);
  95. CRectX rc;
  96. GetWindowRect(m_hWnd,&rc);
  97. BitBlt(hdc,0,0,rc.Width(),rc.Height(),hMemDC,0,0,SRCCOPY);
  98. SelectObject(hMemDC,hob);
  99. DeleteDC(hMemDC);
  100. EndPaint(m_hWnd,&ps);
  101. }
  102. /*------------------------------------------------------------------------------*/
  103. BOOL CCoolBtn::Create(RECT &rc, HWND hParent, UINT uID)
  104. {
  105. return CButtonX::Create(0,WS_CHILD|WS_VISIBLE,rc,hParent,uID);
  106. }
  107. /*------------------------------------------------------------------------------*/
  108. //载入位图
  109. void CCoolBtn::LoadBitmaps(UINT uBmpNormal, UINT uBmpHover, UINT uBmpDown)
  110. {
  111. if(m_hNormal)
  112. DeleteObject(m_hNormal);
  113. if(m_hHover)
  114. DeleteObject(m_hHover);
  115. if(m_hDown)
  116. DeleteObject(m_hDown);
  117. m_hNormal=LoadBitmap(hInstX,(LPCTSTR)uBmpNormal);
  118. m_hHover=LoadBitmap(hInstX,(LPCTSTR)uBmpHover);
  119. m_hDown=LoadBitmap(hInstX,(LPCTSTR)uBmpDown);
  120. }
  121. /*------------------------------------------------------------------------------*/
  122. void CCoolBtn::OnMouseMove(UINT nFlags, POINT point)
  123. {
  124. if(m_uState==0)
  125. {
  126. m_uState=1;
  127. HDC hdc=GetDC(m_hWnd);
  128. HDC hMemDC=CreateCompatibleDC(hdc);
  129. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hHover);
  130. CRectX rc;
  131. GetWindowRect(m_hWnd,&rc);
  132. BitBlt(hdc,0,0,rc.Width(),rc.Height(),hMemDC,0,0,SRCCOPY);
  133. SelectObject(hMemDC,hob);
  134. DeleteDC(hMemDC);
  135. ReleaseDC(m_hWnd,hdc);
  136. SetTimer(m_hWnd,1,50,0);
  137. }
  138. }
  139. /*------------------------------------------------------------------------------*/
  140. void CCoolBtn::OnTimer(UINT nIDEvent)
  141. {
  142. POINT pt;
  143. GetCursorPos(&pt);
  144. RECT rc;
  145. GetWindowRect(m_hWnd,&rc);
  146. if(!PtInRect(&rc,pt))
  147. {
  148. m_uState=0;
  149. HDC hdc=GetDC(m_hWnd);
  150. HDC hMemDC=CreateCompatibleDC(hdc);
  151. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hNormal);
  152. CRectX rc;
  153. GetWindowRect(m_hWnd,&rc);
  154. BitBlt(hdc,0,0,rc.Width(),rc.Height(),hMemDC,0,0,SRCCOPY);
  155. SelectObject(hMemDC,hob);
  156. DeleteDC(hMemDC);
  157. ReleaseDC(m_hWnd,hdc);
  158. KillTimer(m_hWnd,nIDEvent);
  159. }
  160. }
  161. /*------------------------------------------------------------------------------*/
  162. void CCoolBtn::OnLButtonDown(UINT nFlags, POINT &point)
  163. {
  164. m_uState=2;
  165. KillTimer(m_hWnd,1);
  166. SetCapture(m_hWnd);
  167. HDC hdc=GetDC(m_hWnd);
  168. HDC hMemDC=CreateCompatibleDC(hdc);
  169. HBITMAP hob=(HBITMAP)SelectObject(hMemDC,m_hDown);
  170. CRectX rc;
  171. GetWindowRect(m_hWnd,&rc);
  172. BitBlt(hdc,0,0,rc.Width(),rc.Height(),hMemDC,0,0,SRCCOPY);
  173. SelectObject(hMemDC,hob);
  174. DeleteDC(hMemDC);
  175. ReleaseDC(m_hWnd,hdc);
  176. MSG msg;
  177. while(GetMessage(&msg, NULL, 0, 0))
  178. {
  179. if (GetCapture()!=m_hWnd)
  180. {
  181. DispatchMessage(&msg);
  182. break;
  183. }
  184. switch (msg.message)
  185. {
  186. case WM_MOUSEMOVE:
  187. break;
  188. case WM_LBUTTONUP:
  189. {
  190. POINT pt;
  191. GetCursorPos(&pt);
  192. RECT rc;
  193. GetWindowRect(m_hWnd,&rc);
  194. if(PtInRect(&rc,pt))
  195. {
  196. SendMessage(GetParent(m_hWnd),WM_COMMAND,GetDlgCtrlID(m_hWnd),0);
  197. }
  198. }
  199. goto EXITLOOP;
  200. default:
  201. DispatchMessage(&msg);
  202. break;
  203. }
  204. }
  205. EXITLOOP:
  206. m_uState=0;
  207. hdc=GetDC(m_hWnd);
  208. hMemDC=CreateCompatibleDC(hdc);
  209. hob=(HBITMAP)SelectObject(hMemDC,m_hNormal);
  210. GetWindowRect(m_hWnd,&rc);
  211. BitBlt(hdc,0,0,rc.Width(),rc.Height(),hMemDC,0,0,SRCCOPY);
  212. SelectObject(hMemDC,hob);
  213. DeleteDC(hMemDC);
  214. ReleaseDC(m_hWnd,hdc);
  215. ReleaseCapture();
  216. }