CoolButton.cpp
上传用户:onward9999
上传日期:2022-06-27
资源大小:989k
文件大小:6k
源码类别:

其他游戏

开发平台:

Visual C++

  1. // CoolButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CoolButton.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CCoolButton
  12. CCoolButton::CCoolButton()
  13. {
  14. #ifdef XS_FLAT_BUTTON
  15. m_MouseOnButton=FALSE;
  16. #endif
  17. m_hIcon=NULL;
  18. m_cyIcon=0;
  19. m_cxIcon=0;
  20. }
  21. CCoolButton::~CCoolButton()
  22. {
  23. }
  24. BEGIN_MESSAGE_MAP(CCoolButton, CButton)
  25. //{{AFX_MSG_MAP(CCoolButton)
  26. ON_WM_MOUSEMOVE()
  27. ON_WM_KILLFOCUS()
  28. //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CCoolButton message handlers
  32. void CCoolButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
  33. {
  34. // TODO: Add your code to draw the specified item
  35. CDC * pDC=CDC::FromHandle(lpDIS->hDC);
  36. unsigned int  IsPressed =(lpDIS->itemState&ODS_SELECTED);
  37. unsigned int  IsFocused =(lpDIS->itemState&ODS_FOCUS);
  38. unsigned int  IsDisabled =(lpDIS->itemState&ODS_DISABLED);
  39. CRect itemRect = lpDIS->rcItem;
  40. #ifndef XS_FLAT_BUTTON
  41. if(IsFocused)
  42. {
  43. CBrush br(RGB(0,0,0));
  44. pDC->FrameRect(&itemRect,&br);
  45. itemRect.DeflateRect(1,1);
  46. }
  47. #endif
  48. //Fill with bkcolor
  49. CBrush br(GetSysColor(COLOR_BTNFACE));
  50. pDC->FillRect(&itemRect,&br);
  51. //Is pressed?
  52. if(IsPressed)
  53. {
  54. #ifdef XS_FLAT_BUTTON
  55. //浅边界笔
  56. CPen penBtnHiLight(PS_SOLID,0,GetSysColor(COLOR_BTNHILIGHT));
  57. //阴影笔
  58. CPen penBtnShadow(PS_SOLID,0,GetSysColor(COLOR_BTNSHADOW));
  59. //绘边界阴影
  60. pDC->SelectObject(penBtnShadow);
  61. pDC->MoveTo(itemRect.left,itemRect.bottom-1);
  62. pDC->LineTo(itemRect.left,itemRect.top);
  63. pDC->LineTo(itemRect.right,itemRect.top);
  64. //绘浅边界
  65. pDC->SelectObject(penBtnHiLight);
  66. pDC->MoveTo(itemRect.left,itemRect.bottom-1);
  67. pDC->LineTo(itemRect.right-1,itemRect.bottom-1);
  68. pDC->LineTo(itemRect.right-1,itemRect.top-1);
  69. #else
  70. CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
  71. pDC->FrameRect(&itemRect,&brBtnShadow);
  72. #endif
  73. }
  74. else//没按下
  75. {
  76. CPen penBtnHiLight(PS_SOLID,0,GetSysColor(COLOR_BTNHILIGHT));
  77. CPen pen3DLight(PS_SOLID,0,GetSysColor(COLOR_3DLIGHT));
  78. CPen penBtnShadow(PS_SOLID,0,GetSysColor(COLOR_BTNSHADOW));
  79. CPen pen3DDKShadow(PS_SOLID,0,GetSysColor(COLOR_3DDKSHADOW));
  80. #ifdef XS_FLAT_BUTTON
  81. if(m_MouseOnButton==TRUE)
  82. {
  83. pDC->SelectObject(penBtnHiLight);
  84. pDC->MoveTo(itemRect.left,itemRect.bottom-1);
  85. pDC->LineTo(itemRect.left,itemRect.top);
  86. pDC->LineTo(itemRect.right,itemRect.top);
  87. //
  88. pDC->SelectObject(penBtnShadow);
  89. pDC->MoveTo(itemRect.left,itemRect.bottom-1);
  90. pDC->LineTo(itemRect.right-1,itemRect.bottom-1);
  91. pDC->LineTo(itemRect.right-1,itemRect.top-1);
  92. }
  93. #else
  94. pDC->SelectObject(penBtnHiLight);
  95. pDC->MoveTo(itemRect.left,itemRect.bottom-1);
  96. pDC->LineTo(itemRect.left,itemRect.top);
  97. pDC->LineTo(itemRect.right,itemRect.top);
  98. pDC->SelectObject(pen3DLight);
  99. pDC->MoveTo(itemRect.left+1,itemRect.bottom-1);
  100. pDC->LineTo(itemRect.left+1,itemRect.top+1);
  101. pDC->LineTo(itemRect.right,itemRect.top+1);
  102. pDC->SelectObject(pen3DDKShadow);
  103. pDC->MoveTo(itemRect.left,itemRect.bottom-1);
  104. pDC->LineTo(itemRect.right-1,itemRect.bottom-1);
  105. pDC->LineTo(itemRect.right-1,itemRect.top-1);
  106. pDC->SelectObject(penBtnShadow);
  107. pDC->MoveTo(itemRect.left+1,itemRect.bottom-2);
  108. pDC->LineTo(itemRect.right-2,itemRect.bottom-2);
  109. pDC->LineTo(itemRect.right-2,itemRect.top);
  110. #endif
  111. }
  112. #ifndef XS_FLAT_BUTTON
  113. //
  114. if(IsFocused)
  115. {
  116. CRect focusRect = itemRect;
  117. focusRect.DeflateRect(3,3);
  118. pDC->DrawFocusRect(&focusRect);
  119. }
  120. #endif
  121. //获取文本
  122. CString title;
  123. GetWindowText(title);
  124. //绘制图标
  125. if(m_hIcon!=NULL)
  126. {
  127. CRect iconRect = lpDIS->rcItem;
  128. //根据标题是否存在来设置不同的图标位置
  129. if(title.IsEmpty()==TRUE)
  130. {
  131. iconRect.left+=((iconRect.Width()-m_cxIcon)/2);
  132. }
  133. else
  134. {
  135. iconRect.left+=6;
  136. }
  137. iconRect.top+=((iconRect.Height()-m_cyIcon)/2);
  138. if(IsPressed)iconRect.OffsetRect(1,1);
  139. pDC->DrawIcon(iconRect.TopLeft(),m_hIcon);
  140. }
  141. //绘标题
  142. CRect captionRect = lpDIS->rcItem;
  143. captionRect.left+=m_cxIcon;
  144. if(title.IsEmpty()==FALSE)
  145. {
  146. pDC->SetBkMode(TRANSPARENT);
  147. captionRect.OffsetRect(0,-1);
  148. if(IsPressed)
  149. captionRect.OffsetRect(1,1);
  150. if(IsDisabled)
  151. {
  152. captionRect.OffsetRect(1,1);
  153. pDC->SetTextColor(GetSysColor(COLOR_BTNHILIGHT));
  154. pDC->DrawText(title,-1,captionRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  155. captionRect.OffsetRect(-1,-1);
  156. pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
  157. pDC->DrawText(title,-1,captionRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  158. }
  159. else
  160. {
  161. pDC->DrawText(title,-1,captionRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  162. }
  163. }
  164. }
  165. #ifdef XS_FLAT_BUTTON
  166. void CCoolButton::OnMouseMove(UINT nFlags, CPoint point) 
  167. {
  168. // TODO: Add your message handler code here and/or call default
  169. CWnd* pWnd;
  170. CWnd* pParent;
  171. CButton::OnMouseMove(nFlags, point);
  172. pWnd=GetActiveWindow();
  173. pParent=GetOwner();
  174. if((m_MouseOnButton==FALSE)&&(GetCapture()!=this)&&
  175. ((pWnd!=NULL)&&(pWnd->m_hWnd==pParent->m_hWnd)))
  176. {
  177. SetCapture();
  178. SetFocus();
  179. m_MouseOnButton=TRUE;
  180. Invalidate();
  181. UpdateWindow();
  182. }
  183. else
  184. {
  185. CRect rc;
  186. GetClientRect(&rc);
  187. if(!rc.PtInRect(point))
  188. {
  189. m_MouseOnButton=FALSE;
  190. Invalidate();
  191. UpdateWindow();
  192. ReleaseCapture();
  193. }
  194. }
  195. }
  196. #endif
  197. #ifdef XS_FLAT_BUTTON
  198. void CCoolButton::OnKillFocus(CWnd* pNewWnd) 
  199. {
  200. CButton::OnKillFocus(pNewWnd);
  201. // TODO: Add your message handler code here
  202. if(m_MouseOnButton==TRUE)
  203. {
  204. m_MouseOnButton=FALSE;
  205. Invalidate();
  206. UpdateWindow();
  207. }
  208. }
  209. #endif
  210. void CCoolButton::SetIcon(HICON hIcon, BYTE cx, BYTE cy)
  211. {
  212. m_hIcon  = hIcon;
  213. m_cxIcon = cx;
  214. m_cyIcon = cy;
  215. }
  216. BOOL CCoolButton::SubclassDlgItem(UINT nID, CWnd *pParent)
  217. {
  218. BOOL retValue=CButton::SubclassDlgItem(nID,pParent);
  219. LONG bs=::GetWindowLong(m_hWnd,GWL_STYLE);
  220. bs|=BS_OWNERDRAW;
  221. ::SetWindowLong(m_hWnd,GWL_STYLE,bs);
  222. return retValue;
  223. }