MyButton.cpp
上传用户:jzscgs158
上传日期:2022-05-25
资源大小:8709k
文件大小:10k
源码类别:

百货/超市行业

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "math.h"
  3. #include "MyButton.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CMyButton
  11. CMyButton::CMyButton()
  12. {
  13. m_clrText=RGB(0,0,0);
  14. m_clrBack=RGB(161,192,245);
  15. m_clrLeft=RGB(97,244,78);
  16. m_clrRight=RGB(52,148,39);
  17. m_clrFlatBack=GetSysColor(COLOR_3DFACE);
  18. m_blnMouseOn=FALSE;
  19. intStyle=0;
  20. intAlgin=0;
  21. intButtonFace=0;
  22. m_BackBrush.CreateSolidBrush(m_clrFlatBack);
  23. }
  24. CMyButton::~CMyButton()
  25. {
  26. m_rgn.DeleteObject();
  27. DestroyXIcon();
  28. m_BackBrush.DeleteObject();
  29. }
  30. BEGIN_MESSAGE_MAP(CMyButton, CButton)
  31. //{{AFX_MSG_MAP(CMyButton)
  32. ON_WM_MOUSEMOVE()
  33. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  34. //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CMyButton message handlers
  38. void CMyButton::PreSubclassWindow() 
  39. {
  40. CButton::PreSubclassWindow();
  41. ModifyStyle(0, BS_OWNERDRAW);
  42. CRect rect;
  43. GetClientRect(rect);
  44. m_rgn.DeleteObject(); 
  45. SetWindowRgn(NULL, FALSE);
  46. if(intButtonFace==1)
  47. m_rgn.CreateRoundRectRgn(rect.left,rect.top+3,rect.right,rect.bottom,10,10);
  48. if(intButtonFace==0)
  49. m_rgn.CreateRectRgn(rect.left,rect.top+3,rect.right,rect.bottom);
  50. if(intButtonFace==2)
  51. m_rgn.CreateEllipticRgn(rect.left,rect.top+3,rect.right,rect.bottom);
  52. SetWindowRgn(m_rgn, TRUE);
  53. ClientToScreen(rect);
  54. CWnd* pParent = GetParent();
  55. if (pParent) pParent->ScreenToClient(rect);
  56. MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
  57. }
  58. void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  59. {
  60. ASSERT(lpDrawItemStruct != NULL);
  61. CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
  62. CRect rect = lpDrawItemStruct->rcItem;
  63. UINT state = lpDrawItemStruct->itemState;
  64. UINT nStyle = GetStyle();
  65. CRgn rgn1,rgn2;
  66. int r1,g1,b1;
  67. if(intButtonFace==1)
  68. {
  69. rgn1.CreateRoundRectRgn(rect.left,rect.top+3,rect.right,rect.bottom,10,10);
  70. rgn2.CreateRoundRectRgn(rect.left+1,rect.top+4,rect.right-1,rect.bottom-1,10,10);
  71. }
  72. if(intButtonFace==0)
  73. {
  74. rgn1.CreateRectRgn(rect.left,rect.top+3,rect.right,rect.bottom);
  75. rgn2.CreateRectRgn(rect.left+1,rect.top+4,rect.right-1,rect.bottom-1);
  76. }
  77. if(intButtonFace==2)
  78. {
  79. rgn1.CreateEllipticRgn(rect.left,rect.top+3,rect.right,rect.bottom);
  80. rgn2.CreateEllipticRgn(rect.left+1,rect.top+4,rect.right-1,rect.bottom-1);
  81. }
  82. pDC->SelectClipRgn(&rgn1);
  83. int nSavedDC = pDC->SaveDC();
  84. CBrush brush1,brush2;
  85. if(!(nStyle&BS_NOTIFY))
  86. {
  87. if ((state & ODS_FOCUS)||(state & ODS_SELECTED))
  88. {
  89. r1=GetRValue(m_clrBack);
  90. g1=GetGValue(m_clrBack);
  91. b1=GetBValue(m_clrBack);
  92. for(int i=rect.Height()/2;i>0;i--)
  93. {
  94. r1=(r1+10)>255?255:(r1+10);
  95. g1=(g1+10)>255?255:(g1+10);
  96. b1=(b1+10)>255?255:(b1+10);
  97. CPen pen(PS_SOLID, 1, RGB(r1, g1, b1));
  98. CPen *old = pDC->SelectObject(&pen);
  99. pDC->MoveTo(rect.left,rect.top+i);
  100. pDC->LineTo(rect.right,rect.top+i);
  101. pDC->MoveTo(rect.left,rect.bottom-i);
  102. pDC->LineTo(rect.right,rect.bottom-i);
  103. pDC->SelectObject(old);
  104. }
  105. r1=GetRValue(m_clrRight);
  106. g1=GetGValue(m_clrRight);
  107. b1=GetBValue(m_clrRight);
  108. r1=(r1+30)>255?255:(r1+30);
  109. g1=(g1+30)>255?255:(g1+30);
  110. b1=(b1+30)>255?255:(b1+30);
  111. brush2.CreateSolidBrush(m_clrRight);
  112. brush1.CreateSolidBrush(RGB(r1,g1,b1));
  113. pDC->FrameRgn(&rgn1,&brush1,1,1);
  114. pDC->FrameRgn(&rgn2,&brush2,1,1);
  115. }
  116. else if(m_blnMouseOn)
  117. {
  118. r1=GetRValue(m_clrBack);
  119. g1=GetGValue(m_clrBack);
  120. b1=GetBValue(m_clrBack);
  121. for(int i=rect.Height()/2;i>0;i--)
  122. {
  123. r1=(r1+10)>255?255:(r1+10);
  124. g1=(g1+10)>255?255:(g1+10);
  125. b1=(b1+10)>255?255:(b1+10);
  126. CPen pen(PS_SOLID, 1, RGB(r1, g1, b1));
  127. CPen *old = pDC->SelectObject(&pen);
  128. pDC->MoveTo(rect.left,rect.top+i);
  129. pDC->LineTo(rect.right,rect.top+i);
  130. pDC->MoveTo(rect.left,rect.bottom-i);
  131. pDC->LineTo(rect.right,rect.bottom-i);
  132. pDC->SelectObject(old);
  133. }
  134. r1=GetRValue(m_clrLeft);
  135. g1=GetGValue(m_clrLeft);
  136. b1=GetBValue(m_clrLeft);
  137. r1=(r1+30)>255?255:(r1+30);
  138. g1=(g1+30)>255?255:(g1+30);
  139. b1=(b1+30)>255?255:(b1+30);
  140. brush2.CreateSolidBrush(m_clrLeft);
  141. brush1.CreateSolidBrush(RGB(r1,g1,b1));
  142. pDC->FrameRgn(&rgn1,&brush1,1,1);
  143. pDC->FrameRgn(&rgn2,&brush2,1,1);
  144. }
  145. else 
  146. {
  147. if(nStyle&BS_FLAT)
  148. {
  149. CBrush bs;
  150. bs.CreateSolidBrush(m_clrFlatBack);
  151. CRect rcBack;
  152. GetClientRect(rcBack);
  153. pDC->FillRect(rcBack,&m_BackBrush);
  154. }
  155. else
  156. {
  157. r1=GetRValue(m_clrBack);
  158. g1=GetGValue(m_clrBack);
  159. b1=GetBValue(m_clrBack);
  160. for(int i=rect.Height()/2;i>0;i--)
  161. {
  162. r1=(r1+13)>255?255:(r1+13);
  163. g1=(g1+13)>255?255:(g1+13);
  164. b1=(b1+13)>255?255:(b1+13);
  165. CPen pen(PS_SOLID, 1, RGB(r1, g1, b1));
  166. CPen *old = pDC->SelectObject(&pen);
  167. pDC->MoveTo(rect.left,rect.top+i);
  168. pDC->LineTo(rect.right,rect.top+i);
  169. pDC->MoveTo(rect.left,rect.bottom-i);
  170. pDC->LineTo(rect.right,rect.bottom-i);
  171. pDC->SelectObject(old);
  172. }
  173. r1=GetRValue(m_clrBack);
  174. g1=GetGValue(m_clrBack);
  175. b1=GetBValue(m_clrBack);
  176. r1=(r1+30)>255?255:(r1+30);
  177. g1=(g1+30)>255?255:(g1+30);
  178. b1=(b1+30)>255?255:(b1+30);
  179. brush2.CreateSolidBrush(m_clrBack);
  180. brush1.CreateSolidBrush(RGB(r1,g1,b1));
  181. pDC->FrameRgn(&rgn1,&brush1,1,1);
  182. pDC->FrameRgn(&rgn2,&brush2,1,1);
  183. }
  184. }
  185. }
  186. //输出文字
  187. CFont *ft=GetFont();
  188. LOGFONT logft;
  189. ft->GetLogFont(&logft);
  190. CRect rcIcon(rect);
  191. if(intStyle==1)
  192. {
  193. switch(intAlgin)
  194. {
  195. case 1: //文字在下端
  196. rcIcon.bottom=rect.bottom-abs(logft.lfHeight);
  197. if(rcIcon.bottom<0) rcIcon.bottom=0;
  198. DrawIconX(pDC,rcIcon);
  199. rect.top=rcIcon.bottom-(rcIcon.Height()-dwIconHeight)/2;
  200. break;
  201. case 2://文字在上
  202. rcIcon.top=abs(logft.lfHeight)+rect.top;
  203. DrawIconX(pDC,rcIcon);
  204. rect.bottom=rcIcon.top+(rcIcon.Height()-dwIconHeight)/2;
  205. break;
  206. case 3://文字在左
  207. GetXiconInf();
  208. rcIcon.left=rect.Width()*0.9+rect.left-dwIconWidth-5;
  209. rcIcon.right=rect.Width()*0.9+rect.left;
  210. DrawIconX(pDC,rcIcon);
  211. rect.right=rcIcon.left;
  212. break;
  213. case 0://文字在右
  214. default:
  215. DrawIconX(pDC,rcIcon);
  216. rect.left=dwIconWidth+rect.left+rect.Width()*0.1;
  217. break;
  218. }
  219. }
  220. else
  221. {
  222. switch(intAlgin)
  223. {
  224. case 1: //文字在下端
  225. rect.top=rect.bottom-abs(logft.lfHeight)-10;
  226. break;
  227. case 2://文字在上
  228. rect.bottom=abs(logft.lfHeight)+10+rect.top;
  229. break;
  230. case 3://文字在左
  231. case 0://文字在右
  232. default:
  233. break;
  234. }
  235. }
  236. CPoint m_ptCentre=rect.CenterPoint();
  237. CString strText;
  238. GetWindowText(strText);
  239. if (!strText.IsEmpty())
  240. {
  241. pDC->SetTextColor(m_clrText);
  242. CSize Extent = pDC->GetTextExtent(strText);
  243. CPoint pt = CPoint( m_ptCentre.x - Extent.cx/2, m_ptCentre.y - Extent.cy/2 );
  244. if (state & ODS_SELECTED) pt.Offset(1,1);
  245. pDC->SetBkMode(TRANSPARENT);
  246. if ((state & ODS_DISABLED)&!(nStyle&BS_NOTIFY))
  247. pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
  248. else if(nStyle&BS_NOTIFY)
  249. {
  250. pDC->FillRect(rect,&m_BackBrush);
  251. if((state & ODS_FOCUS)||(state & ODS_SELECTED))
  252. {
  253. pDC->SetTextColor(m_clrRight);
  254. pDC->TextOut(pt.x+2, pt.y+2, strText);
  255. }
  256. else if(m_blnMouseOn)
  257. {
  258. pDC->SetTextColor(m_clrLeft);
  259. pDC->TextOut(pt.x+2, pt.y+2, strText);
  260. }
  261. else
  262. {
  263. pDC->TextOut(pt.x, pt.y, strText);
  264. }
  265. }
  266. else
  267. {
  268. pDC->TextOut(pt.x, pt.y, strText);
  269. }
  270. }
  271. pDC->SelectClipRgn(NULL);
  272. pDC->RestoreDC(nSavedDC);
  273. rgn1.DeleteObject();
  274. rgn2.DeleteObject();
  275. }
  276. void CMyButton::SetMaskColor(COLORREF colLeft,COLORREF colRight)
  277. {
  278. m_clrRight=colRight;
  279. m_clrLeft=colLeft;
  280. this->Invalidate(TRUE);
  281. }
  282. void CMyButton::SetBackColor(COLORREF color)
  283. {
  284. m_clrBack=color;
  285. this->Invalidate(TRUE);
  286. }
  287. void CMyButton::SetBackImage(UINT nIDResource)
  288. {
  289. CBitmap bmp;
  290. intStyle=3;
  291. bmp.LoadBitmap(nIDResource);
  292. m_BackBrush.DeleteObject();
  293. m_BackBrush.CreatePatternBrush(&bmp);
  294. }
  295. void CMyButton::SetButtonFace(int intStyle)
  296. {
  297. intButtonFace=intStyle;
  298. this->Invalidate();
  299. }
  300. void CMyButton::SetFlatBack(COLORREF colFlat)
  301. {
  302. m_clrFlatBack=colFlat;
  303. m_BackBrush.DeleteObject();
  304. m_BackBrush.CreateSolidBrush(colFlat);
  305. this->Invalidate(TRUE);
  306. }
  307. void CMyButton::SetTextColor(COLORREF color)
  308. {
  309. m_clrText=color;
  310. this->Invalidate(TRUE);
  311. }
  312. void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
  313. {
  314. CWnd* wndUnderMouse = NULL;
  315. CWnd* wndActive = this;
  316. TRACKMOUSEEVENT csTME;
  317. CButton::OnMouseMove(nFlags, point);
  318. ClientToScreen(&point);
  319. wndUnderMouse = WindowFromPoint(point);
  320. // If the mouse enter the button with the left button pressed then do nothing
  321. if (nFlags & MK_LBUTTON) return;
  322. // If our button is not flat then do nothing
  323. wndActive = GetActiveWindow();
  324. if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
  325. {
  326. if (!m_blnMouseOn)
  327. {
  328. m_blnMouseOn = TRUE;
  329. Invalidate();
  330. csTME.cbSize = sizeof(csTME);
  331. csTME.dwFlags = TME_LEAVE;
  332. csTME.hwndTrack = m_hWnd;
  333. ::_TrackMouseEvent(&csTME);
  334. }
  335. else
  336. {
  337. m_blnMouseOn=FALSE;
  338. Invalidate();
  339. }
  340. }
  341. LRESULT CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  342. {
  343. m_blnMouseOn=FALSE;
  344. Invalidate();
  345. return 0;
  346. }
  347. void CMyButton::SetXIcon(UINT nIDResource)
  348. {
  349. intStyle=1;
  350. Xicon=AfxGetApp()->LoadIcon(nIDResource);
  351. this->Invalidate();
  352. }
  353. void CMyButton::DrawIconX(CDC *pDC,CRect rcItem)
  354. {
  355. GetXiconInf();
  356. int intTop=(rcItem.Height()-dwIconHeight)/2;
  357. if(intTop<0) intTop=0;
  358. intTop=rcItem.top+intTop;
  359. if(intAlgin==0)
  360. {
  361. int intLeft=rcItem.left+rcItem.Width()*0.1;
  362. pDC->DrawIcon(intLeft,intTop,Xicon);
  363. }
  364. else
  365. {
  366. int intLeft=(rcItem.Width()-dwIconWidth)/2;
  367. if(intLeft<0) intLeft=0;
  368. intLeft=intLeft+rcItem.left;
  369. pDC->DrawIcon(intLeft,intTop,Xicon);
  370. }
  371. }
  372. void CMyButton::DestroyXIcon()
  373. {
  374. if(Xicon) ::DestroyIcon(Xicon);
  375. }
  376. void CMyButton::GetXiconInf()
  377. {
  378. BOOL bRetValue;
  379. ICONINFO ii;
  380. if (Xicon)
  381. {
  382. ::ZeroMemory(&ii, sizeof(ICONINFO));
  383. bRetValue = ::GetIconInfo(Xicon, &ii);
  384. if (bRetValue == FALSE)
  385. {
  386. DestroyXIcon();
  387. } // if
  388. dwIconWidth = (DWORD)(ii.xHotspot * 2);
  389. dwIconHeight= (DWORD)(ii.yHotspot * 2);
  390. ::DeleteObject(ii.hbmMask);
  391. ::DeleteObject(ii.hbmColor);
  392. }
  393. }
  394. void CMyButton::SetXAlign(int Align)
  395. {
  396. intAlgin=Align;
  397. this->Invalidate();
  398. }
  399. void CMyButton::SetXStyle(int Style)
  400. {
  401. intStyle=Style;
  402. this->Invalidate();
  403. }