MyButton.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include "stdafx.h"
  2. #include "MyButton.h"
  3. #include "ButtonGroup.h"
  4. #include <atlimage.h>
  5. IMPLEMENT_DYNAMIC(CMyButton, CButton)
  6. CMyButton::CMyButton():
  7. btnState(0)
  8. ,colorState(0)
  9. ,lastColorState(0)
  10. ,chooseState(0)
  11. ,strInfo("")
  12. {
  13. }
  14. CMyButton::~CMyButton()
  15. {
  16. }
  17. BEGIN_MESSAGE_MAP(CMyButton, CButton)
  18. ON_CONTROL_REFLECT_EX(BN_CLICKED,OnClicked)
  19. ON_WM_MOUSEMOVE()
  20. ON_WM_LBUTTONDOWN()
  21. ON_WM_LBUTTONUP()
  22. ON_WM_PAINT()
  23. ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
  24. END_MESSAGE_MAP()
  25. void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
  26. {
  27. CButton::OnMouseMove(nFlags, point);
  28. CRect rc;
  29. GetClientRect(&rc);
  30. if(rc.PtInRect(point))
  31. {
  32. TRACKMOUSEEVENT tme;
  33. tme.cbSize = sizeof(tme);
  34. tme.hwndTrack = m_hWnd;
  35. tme.dwFlags = TME_LEAVE | TME_HOVER;
  36. tme.dwHoverTime = 1;
  37. _TrackMouseEvent(&tme);
  38. if (btnState == 0 && chooseState == 0)
  39. {
  40. colorState = 0;//鼠标没有按下且没被选中时btn不改变颜色
  41. }
  42. else if (btnState == 1)
  43. {
  44. colorState = 1;
  45. }
  46. }
  47. else
  48. {
  49. colorState = 0;
  50. }
  51. if (lastColorState != colorState)
  52. {
  53. Invalidate( false );
  54. lastColorState = colorState;
  55. }
  56. }
  57. LRESULT CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  58. {
  59. if (type == 0)
  60. {
  61. colorState = 0;
  62. }
  63. else if (type == 1)
  64. {
  65. }
  66. if (lastColorState != colorState)
  67. {
  68. Invalidate( false );
  69. lastColorState = colorState;
  70. }
  71. return 0;
  72. }
  73. void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
  74. {
  75. btnState = 1;
  76. colorState = 1;
  77. CButton::OnLButtonDown(nFlags, point);
  78. }
  79. void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
  80. {
  81. btnState = 0;
  82. if (type == 0)
  83. {
  84. colorState = 0;
  85. }
  86. else if (type == 1)
  87. {
  88. CRect rc;
  89. GetClientRect(&rc);
  90. if(rc.PtInRect(point))
  91. {
  92. colorState = 1;
  93. }
  94. else
  95. {
  96. colorState = 0;
  97. }
  98. }
  99. CButton::OnLButtonUp(nFlags, point);
  100. }
  101. BOOL CMyButton::OnClicked()
  102. {
  103. pParent->handleClick(index);
  104. return FALSE;
  105. }
  106. void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  107. {
  108. drawButton();
  109. }
  110. void CMyButton::drawButton()
  111. {
  112. CClientDC dc(this);
  113. dc.SetBkMode( TRANSPARENT );
  114. CRect rc;
  115. GetClientRect(rc);
  116. CFont BigFont, NozFont, ItemFont;
  117. CFont *pFont = NULL;
  118. int nBigFontH = 30;
  119. int nNozFontH = 19;
  120. int nItemFoldH = 16;
  121. if( 0 != NozFont.CreateFont( nNozFontH, 0,0,0, FW_NORMAL ,0,0,0,DEFAULT_CHARSET,
  122. OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
  123. VARIABLE_PITCH|FF_SWISS, _T("微软雅黑") ) )
  124. {
  125. pFont = dc.SelectObject(&NozFont);
  126. }
  127. CPen bgPen;
  128. bgPen.CreatePen( PS_SOLID,1,RGB( 255,255,255 ) );
  129. CPen* pOldPen = dc.SelectObject( &bgPen );
  130. CBrush brh1;
  131. if (colorState == 0)
  132. {
  133. brh1.CreateSolidBrush(RGB(255,255,255));
  134. }
  135. else if (colorState == 1)
  136. {
  137. brh1.CreateSolidBrush(RGB(117,197,240));
  138. }
  139. CBrush *pOldBrush = dc.SelectObject( &brh1 );
  140. CBrush brh2;
  141. CFont font;
  142. font.CreateFont(-MulDiv(int(18),72,72),
  143. 0,0,0,FW_MEDIUM,0,0,0,GB2312_CHARSET,
  144. OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,PROOF_QUALITY,
  145. FIXED_PITCH|FF_SWISS, "微软雅黑" );
  146. dc.SelectObject(&font);
  147. COLORREF color;
  148. TRIVERTEX vert[2] ;
  149. GRADIENT_RECT gRect;
  150. vert [0] .x = rc.left;
  151. vert [0] .y = rc.top;
  152. vert [1] .x = rc.right;
  153. vert [1] .y = rc.bottom;
  154. if (colorState == 1 || strInfo.Find(_T("已选")) != -1)
  155. {
  156. //绿 (194,239,150) (118,195,44)
  157. // vert [0] .Red = 0xc200;
  158. // vert [0] .Green = 0xef00;
  159. // vert [0] .Blue = 0x9600;
  160. // vert [0] .Alpha = 0xff00;
  161. //
  162. // vert [1] .Red = 0x7600;
  163. // vert [1] .Green = 0xc300;
  164. // vert [1] .Blue = 0x2c00;
  165. // vert [1] .Alpha = 0xff00;
  166. //蓝2 (15,220,247) (0,87,127)
  167. vert [0] .Red = 0x0f00;
  168. vert [0] .Green = 0xdc00;
  169. vert [0] .Blue = 0xf700;
  170. vert [0] .Alpha = 0xff00;
  171. vert [1] .Red = 0x0000;
  172. vert [1] .Green = 0x5700;
  173. vert [1] .Blue = 0x7f00;
  174. vert [1] .Alpha = 0xff00;
  175. brh2.CreateSolidBrush(RGB(0,87,127));
  176. color = RGB(18,71,91);
  177. }
  178. else if (colorState == 0)
  179. {
  180. if (index == 0)
  181. {
  182. //橙 (243,196,107) (252,152,0)
  183. vert [0] .Red = 0xf300;
  184. vert [0] .Green = 0xc400;
  185. vert [0] .Blue = 0x6b00;
  186. vert [0] .Alpha = 0xff00;
  187. vert [1] .Red = 0xfc00;
  188. vert [1] .Green = 0x9800;
  189. vert [1] .Blue = 0x0000;
  190. vert [1] .Alpha = 0xff00;
  191. brh2.CreateSolidBrush(RGB(252,152,0));
  192. color = RGB(88,36,27);
  193. }
  194. else if (index >= 1 && index <= 2)
  195. {
  196. //蓝 (150,196,235) (39,148,228)
  197. vert [0] .Red = 0x9600;
  198. vert [0] .Green = 0xc400;
  199. vert [0] .Blue = 0xeb00;
  200. vert [0] .Alpha = 0xff00;
  201. vert [1] .Red = 0x2700;
  202. vert [1] .Green = 0x9400;
  203. vert [1] .Blue = 0xe400;
  204. vert [1] .Alpha = 0xff00;
  205. brh2.CreateSolidBrush(RGB(51,51,153));
  206. color = RGB(51,51,153);
  207. }
  208. else if (index >= 3 && index <= 5)
  209. {
  210. //黄 (239,240,113) (231,159,67)
  211. vert [0] .Red = 0xef00;
  212. vert [0] .Green = 0xf000;
  213. vert [0] .Blue = 0x7100;
  214. vert [0] .Alpha = 0xff00;
  215. vert [1] .Red = 0xe700;
  216. vert [1] .Green = 0xc300;
  217. vert [1] .Blue = 0x4300;
  218. vert [1] .Alpha = 0xff00;
  219. brh2.CreateSolidBrush(RGB(231,159,67));
  220. color = RGB(51,47,3);
  221. }
  222. else if (index >= 6 && index <= 9)
  223. {
  224. //深 (178,202,224) (137,160,175)
  225. vert [0] .Red = 0xb200;
  226. vert [0] .Green = 0xca00;
  227. vert [0] .Blue = 0xe000;
  228. vert [0] .Alpha = 0xff00;
  229. vert [1] .Red = 0x8900;
  230. vert [1] .Green = 0xa000;
  231. vert [1] .Blue = 0xaf00;
  232. vert [1] .Alpha = 0xff00;
  233. brh2.CreateSolidBrush(RGB(137,160,175));
  234. color = RGB(38,45,49);
  235. }
  236. else
  237. {
  238. //灰 229 169
  239. vert [0] .Red = 0xe500;
  240. vert [0] .Green = 0xe500;
  241. vert [0] .Blue = 0xe500;
  242. vert [0] .Alpha = 0xff00;
  243. vert [1] .Red = 0xa900;
  244. vert [1] .Green = 0xa900;
  245. vert [1] .Blue = 0xa900;
  246. vert [1] .Alpha = 0xff00;
  247. brh2.CreateSolidBrush(RGB(169,169,169));
  248. color = RGB(50,50,50);
  249. }
  250. }
  251. gRect.UpperLeft = 0;
  252. gRect.LowerRight = 1;
  253. GradientFill(dc.m_hDC,vert,2,&gRect,1,GRADIENT_FILL_RECT_V);
  254. pOldBrush = dc.SelectObject( &brh2 );
  255. CRgn rgn;
  256. rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,15,15);
  257. dc.FrameRgn(&rgn,&brh2,1,1);
  258. CRgn rgn3;
  259. rgn3.CreateRoundRectRgn(rc.left-5,rc.top-5,rc.right+5,rc.bottom+5,15,15);
  260. CBrush brh3(RGB( 51,55,68));
  261. dc.FrameRgn(&rgn3,&brh3,5,5);
  262. CRect rcBlank(13,75,162,102);
  263. CBrush brh4(RGB( 255,255,255));
  264. pOldBrush = dc.SelectObject( &brh4 );
  265. if (type == 0)
  266. {
  267. dc.RoundRect(rcBlank,CPoint(8,8));
  268. }
  269. CBrush brh(RGB( 57,171,229));
  270. pOldBrush = dc.SelectObject( &brh );
  271. dc.SetTextColor(color);
  272. CRect rcStr(rc);
  273. if (type == 0)
  274. {
  275. rcStr.bottom = 74;
  276. if (strInfo != "")
  277. {
  278. CRect rcText(rcBlank);
  279. rcText.OffsetRect(0,2);
  280. dc.DrawText(strInfo,rcText,DT_CENTER|DT_VCENTER);
  281. }
  282. }
  283. drawlines(&dc,name,rcStr,14,3,22);
  284. dc.SelectObject( pOldBrush );
  285. dc.SelectObject( pOldPen );
  286. bgPen.DeleteObject();
  287. brh1.DeleteObject();
  288. brh2.DeleteObject();
  289. brh.DeleteObject();
  290. NozFont.DeleteObject();
  291. }
  292. void CMyButton::drawlines(CDC* pDC, CString str, CRect rc, UINT charNumOfLine, UINT linenum, UINT rowHeight)
  293. {
  294. vector<CString> vStrs;
  295. CString tmpStr = str;
  296. while (tmpStr!="" && vStrs.size() < linenum)
  297. {
  298. int pos = getDividePos(tmpStr,charNumOfLine);
  299. CString tmpStr1 = tmpStr.Left(pos);
  300. vStrs.push_back(tmpStr1);
  301. if (tmpStr1 == tmpStr)
  302. {
  303. tmpStr = "";
  304. }
  305. else
  306. {
  307. tmpStr = tmpStr.Mid(pos);
  308. }
  309. }
  310. UINT headAlign = (rc.bottom - rc.top - vStrs.size() * rowHeight) / 2;
  311. CRect rc1(rc);
  312. rc1.top += headAlign;
  313. rc1.left += 5;
  314. rc1.right -=5;
  315. for (size_t i = 0; i < vStrs.size(); i++)
  316. {
  317. rc1.bottom = rc1.top + rowHeight;
  318. pDC->DrawText(vStrs[i],rc1,DT_CENTER|DT_VCENTER);
  319. rc1.top += rowHeight;
  320. }
  321. }
  322. int CMyButton::getDividePos(CString str, int num)
  323. {
  324. if( str.GetLength() > num )
  325. {
  326. int numOfChineseChar = 0;
  327. for ( int i = 0; i < num; i ++ )
  328. {
  329. if ( str[i] < 0 || str[i] > 255 )
  330. {
  331. numOfChineseChar ++;
  332. }
  333. }
  334. if ( numOfChineseChar % 2 == 0 )
  335. {
  336. if (str.GetAt(num-1) == 'm')
  337. {
  338. return num - 1;
  339. }
  340. else
  341. {
  342. return num;
  343. }
  344. }
  345. else
  346. {
  347. if (str.GetAt(num-1) == 'm')
  348. {
  349. return num - 2;
  350. }
  351. else
  352. {
  353. return num-1;
  354. }
  355. }
  356. }
  357. else
  358. {
  359. return str.GetLength();
  360. }
  361. }
  362. void CMyButton::changeColor(bool change)
  363. {
  364. if (change)
  365. {
  366. chooseState = 1;
  367. colorState = 1;
  368. }
  369. else
  370. {
  371. chooseState = 0;
  372. colorState = 0;
  373. }
  374. if (lastColorState != colorState)
  375. {
  376. Invalidate( false );
  377. lastColorState = colorState;
  378. }
  379. }
  380. void CMyButton::OnPaint()
  381. {
  382. CButton::OnPaint();
  383. }