GuiButton.cpp
上传用户:zhanglf88
上传日期:2013-11-19
资源大小:6036k
文件大小:24k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------//
  2. // This is a part of the GuiLib MFC Extention.  //
  3. // Autor  :  Francisco Campos  //
  4. // (C) 2002 Francisco Campos <www.beyondata.com> All rights reserved     //
  5. // This code is provided "as is", with absolutely no warranty expressed  //
  6. // or implied. Any use is at your own risk.  //
  7. // You must obtain the author's consent before you can include this code //
  8. // in a software library.  //
  9. // If the source code in  this file is used in any application  //
  10. // then acknowledgement must be made to the author of this program  //
  11. // fcampos@tutopia.com  //
  12. //-----------------------------------------------------------------------//
  13. #include "stdafx.h"
  14. #include "GuiButton.h"
  15. #include "resource.h"
  16. #include "GuiDrawLayer.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. #define   OVER     0
  23. #define   FOCUS    1
  24. #define   NORMAL   2
  25. #define   DISABLED 3
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CGuiButton
  28. CGuiButton::CGuiButton()
  29. {
  30. m_iAling=ALING_LEFT; 
  31. m_iStyle=STYLE_SEMIFLAT;
  32. m_lpMsg="";
  33. m_iSelected=0;
  34. m_viewImage=FALSE;
  35. m_ViewText=FALSE;
  36. m_iTypeButton =TYPE_NORMAL; //Normal by default
  37. m_iMenuActivo=FALSE;
  38. m_clrface=GuiDrawLayer::GetRGBColorFace();
  39. m_ReposWindow=FALSE;
  40. m_bIsDark=FALSE;
  41. /*Modified By SunZhenyu 2004/08/24, add the next 2 line */
  42. m_iStyle=STYLEXP;
  43. m_ViewText=TRUE;
  44. }
  45. CGuiButton::~CGuiButton()
  46. {
  47. }
  48. BEGIN_MESSAGE_MAP(CGuiButton, CButton)
  49. //{{AFX_MSG_MAP(CGuiButton)
  50. ON_WM_CREATE()
  51. ON_WM_ERASEBKGND()
  52. ON_WM_MOUSEMOVE()
  53. ON_WM_LBUTTONUP()
  54. ON_WM_LBUTTONDOWN()
  55. ON_WM_TIMER()
  56. ON_WM_SYSCOLORCHANGE()
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CGuiButton message handlers
  61. //*****************************************************************************
  62. void CGuiButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  63. {
  64. // TODO: Add your code to draw the specified item
  65. ICONINFO bm;
  66. CSize sizeImag;
  67. CBrush cb;
  68. CSize SizeCad=CSize(0,0);
  69. CSize pt;
  70. CPoint m_point;
  71. int m_Index=0;
  72. ZeroMemory(&bm, sizeof(ICONINFO));
  73. CDC* pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
  74. CRect m_rect=&lpDrawItemStruct->rcItem;
  75. UINT m_State=lpDrawItemStruct->itemState;
  76. //---------------------------------------------------------------------
  77. //Se obtiene la cadena del boton si tiene
  78. //---------------------------------------------------------------------
  79. CString m_caption;
  80. GetWindowText(m_caption);
  81. //---------------------------------------------------------------------
  82. // Se dibuja el fondo segun sea el estilo
  83. // si es TooBarXP
  84. // si no si es un boton Normal
  85. //---------------------------------------------------------------------
  86. if (m_State & ODS_SELECTED) //el boton esta presionado
  87. {
  88.   if (STYLEXP == m_iStyle) 
  89. cb.CreateSolidBrush(m_clrface);
  90. }
  91. else if(m_iSelected == 1) //el mouse esta sobre el boton
  92. {
  93.   if (STYLEXP == m_iStyle) 
  94.   cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorBTNHigh());
  95. }
  96. else //el boton esta normal
  97. {
  98.   if (STYLEXP == m_iStyle) 
  99. cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorBTNHigh());
  100. }
  101. if (STYLEXP == m_iStyle) 
  102. {
  103. CRect rcTemp=m_rect;
  104. rcTemp.DeflateRect(2,2);
  105. rcTemp.right-=2;
  106. pDC->FillRect(&rcTemp,&cb);
  107. }
  108. else
  109. {
  110. cb.CreateSolidBrush(GuiDrawLayer::GetRGBColorFace());
  111. pDC->FillRect(&m_rect,&cb);
  112. }
  113. //********************************************************************
  114. int nMode = pDC->SetBkMode(TRANSPARENT);
  115. if (m_caption.GetLength() > 0)
  116. SizeCad=pDC->GetTextExtent(m_caption);
  117. //-------------------------------------------------------------------
  118. //si el boton esta seleccionado, se mueve el rectangulo para dar la 
  119. //impresion de presion
  120. //-------------------------------------------------------------------
  121. if (m_State & ODS_SELECTED)
  122. m_rect.bottom+=2;
  123. //-------------------------------------------------------------------
  124. //El boton tiene una imagen
  125. //-------------------------------------------------------------------
  126. if (m_viewImage == TRUE)
  127. {
  128. if (m_State & ODS_SELECTED)
  129. m_Index=2;
  130. else if (m_iSelected== 1)
  131. m_Index=1;
  132. else
  133. m_Index=0;
  134. if (m_hicon[m_Index]!= NULL) 
  135. {
  136. ::GetIconInfo(m_hicon[m_Index], &bm);
  137. sizeImag=CSize((BYTE)(bm.xHotspot * 2),(BYTE)(bm.yHotspot * 2));
  138. }
  139. else m_Index=-1;
  140. }
  141. //---------------------------------------------------------------------
  142. //el boton tiene una imagen pero no texto
  143. //---------------------------------------------------------------------
  144. if (m_viewImage == TRUE && m_ViewText== FALSE)
  145. {
  146. if (m_Index != -1)
  147. {
  148. int calculodify;
  149. m_rect.right=m_rect.left+sizeImag.cx+5;
  150. calculodify=m_rect.Height()-(sizeImag.cy);
  151. calculodify/=2;
  152. m_point=CPoint(2,calculodify);
  153. if(m_bIsDark && m_iSelected == 1 && !(m_State & ODS_DISABLED))
  154. {
  155. CPoint p(m_point.x+1,m_point.y+1);
  156. pDC->DrawState(p,sizeImag,m_hicon[m_Index],DSS_MONO,CBrush (GuiDrawLayer::GetRGBColorShadow()));
  157. m_point.x-=1; m_point.y-=1;
  158. }
  159. pDC->DrawState (m_point, sizeImag,m_hicon[m_Index],(m_State==ODS_DISABLED?DSS_DISABLED:DSS_NORMAL),CBrush (GuiDrawLayer::GetRGBColorFace()));
  160. }
  161. }
  162. //-----------------------------------------------------------------------
  163. //El boton tiene una imagen y ademas texto
  164. //-----------------------------------------------------------------------
  165. else if (m_viewImage == TRUE && m_ViewText== TRUE)
  166. {
  167. int calculodifx;
  168. int calculodify;
  169. if (m_Index != -1)
  170. {
  171. CRect rectletra;
  172. calculodifx=m_rect.Width()-(sizeImag.cx);
  173. calculodifx/=2;
  174. calculodify=m_rect.Height()-(sizeImag.cy);
  175. calculodify/=2;
  176. if (m_iAling ==ALING_RIGTH)
  177. {
  178. m_point=CPoint(4,calculodify);
  179. if(m_bIsDark && m_iSelected == 1 && !(m_State & ODS_DISABLED))
  180. {
  181. CPoint p(m_point.x+1,m_point.y+1);
  182. pDC->DrawState(p,sizeImag,m_hicon[m_Index],DSS_MONO,CBrush (GuiDrawLayer::GetRGBColorShadow()));
  183. m_point.x-=1; m_point.y-=1;
  184. }
  185. pDC->DrawState (m_point, sizeImag,m_hicon[m_Index],
  186.    (m_State==ODS_DISABLED?DSS_DISABLED:DSS_NORMAL),(CBrush*)NULL);
  187. rectletra=m_rect;
  188. rectletra.left+=sizeImag.cx+8;
  189. pt=CSize(rectletra.top,rectletra.left);
  190. if (m_State & ODS_DISABLED)
  191. pDC->DrawState(pt, SizeCad, m_caption, DSS_DISABLED, TRUE, 0, (CBrush*)NULL);
  192. else
  193. pDC->DrawText(m_caption,rectletra,DT_SINGLELINE|DT_LEFT|DT_VCENTER);
  194. }
  195. else
  196. {
  197. rectletra=m_rect;
  198. rectletra.left+=4;
  199. /* Modified By Sunzhenyu, 2003/8/8, Added the next 1 line
  200. */
  201. m_point=CPoint(4,calculodify);
  202. if(m_bIsDark && m_iSelected == 1 && !(m_State & ODS_DISABLED))
  203. {
  204. CPoint p=CPoint(m_point.x+1,m_point.y+1);
  205. pDC->DrawState(p,sizeImag,m_hicon[m_Index],DSS_MONO,CBrush (GuiDrawLayer::GetRGBColorShadow()));
  206. m_point.x-=1; m_point.y-=1;
  207. }
  208. pt=CSize(rectletra.top,rectletra.left);
  209. if (m_State & ODS_DISABLED)
  210. pDC->DrawState(pt, SizeCad, m_caption, DSS_DISABLED, TRUE, 0, (CBrush*)NULL);
  211. else
  212. pDC->DrawText(m_caption,rectletra,DT_SINGLELINE|DT_LEFT|DT_VCENTER);
  213. m_point=CPoint((rectletra.right-sizeImag.cx)-4,calculodify);
  214. pDC->DrawState (m_point, sizeImag,m_hicon[m_Index],(m_State==ODS_DISABLED?DSS_DISABLED:DSS_NORMAL),(CBrush*)NULL);
  215. }
  216. }
  217.  
  218. }
  219. //-----------------------------------------------------------------------
  220. //Si el texto es visible
  221. //-----------------------------------------------------------------------
  222. else if (m_ViewText== TRUE)
  223. {
  224. CSize sz=pDC->GetTextExtent(m_caption);
  225. int ntempcx=m_rect.Width()-sz.cx;
  226. int ntempcy=m_rect.Height()-sz.cy;
  227. pt=CSize(m_rect.left+(ntempcx/2),m_rect.top+(ntempcy/2));
  228. if (m_State & ODS_DISABLED)
  229. pDC->DrawState(pt, SizeCad, m_caption, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
  230. else
  231. pDC->DrawText(m_caption,m_rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  232. }
  233. m_rect=&lpDrawItemStruct->rcItem;
  234. //-----------------------------------------------------------------
  235. //se inicia el dibujo de bordes 
  236. //1) si es semiflat
  237. //2) si es Flat
  238. //3) si es estilo ToolBarXp  
  239. //4) si es estilo 3d
  240. //-----------------------------------------------------------------
  241. if (m_State & ODS_SELECTED)
  242. {
  243. if (STYLE_SEMIFLAT== m_iStyle)
  244. pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorBTNHigh());
  245. else if (STYLE_FLAT==m_iStyle)
  246. pDC->Draw3dRect(m_rect, GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorBTNHigh());
  247. else if(STYLEXP == m_iStyle)
  248. DrawBottonXP(pDC,m_rect,NORMAL);
  249. else //si es 3d
  250. {
  251. pDC->Draw3dRect(m_rect, GuiDrawLayer::GetRGBColorShadow(),GuiDrawLayer::GetRGBColorBTNHigh());
  252. CPen cp(PS_SOLID,1,GuiDrawLayer::GetRGBColorShadow());
  253. CPen* cpa=pDC->SelectObject(&cp);
  254. pDC->MoveTo(m_rect.left+1,m_rect.top+1);
  255. pDC->LineTo(m_rect.right-1,m_rect.top+1);
  256. pDC->SelectObject(cpa);
  257. }
  258. }
  259. //-----------------------------------------------------------------------
  260. //el boton esta presionado
  261. //-----------------------------------------------------------------------
  262. else if (m_iSelected == 1)  //el mouse esta sobre el boton
  263. {
  264. if (STYLE_SEMIFLAT== m_iStyle)
  265. pDC->Draw3dRect(m_rect, GuiDrawLayer::GetRGBColorBTNHigh(),GuiDrawLayer::GetRGBColorShadow());
  266. else if (STYLE_FLAT==m_iStyle)
  267. pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorBTNHigh(), GuiDrawLayer::GetRGBColorShadow());
  268. else if(STYLEXP == m_iStyle)
  269. DrawBottonXP(pDC,m_rect,OVER);
  270. else //si es 3d
  271. {
  272. pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorBTNHigh(), GuiDrawLayer::GetRGBColorShadow());
  273. CPen cp(PS_SOLID,1,GuiDrawLayer::GetRGBColorShadow());
  274. CPen* cpa=pDC->SelectObject(&cp);
  275. pDC->MoveTo(m_rect.left+1,m_rect.bottom-2);
  276. pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
  277. pDC->MoveTo(m_rect.right-2,m_rect.top+1);
  278. pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
  279. pDC->SelectObject(cpa);
  280. }
  281. }else
  282. {
  283. //-----------------------------------------------------------------
  284. //el boton esta en estado normal, es decir el mouse ni lo presiona
  285. //ni esta sobre el.
  286. //-----------------------------------------------------------------
  287. if (STYLE_3D==m_iStyle)
  288. {
  289. pDC->Draw3dRect(m_rect,GuiDrawLayer::GetRGBColorBTNHigh(), GuiDrawLayer::GetRGBColorShadow());
  290. CPen cp(PS_SOLID,1,GuiDrawLayer::GetRGBColorShadow());
  291. CPen* cpa=pDC->SelectObject(&cp);
  292. pDC->MoveTo(m_rect.left+1,m_rect.bottom-2);
  293. pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
  294. pDC->MoveTo(m_rect.right-2,m_rect.top+1);
  295. pDC->LineTo(m_rect.right-2,m_rect.bottom-2);
  296. pDC->SelectObject(cpa);
  297. }
  298. else if(STYLEXP == m_iStyle)
  299. {
  300. if (m_State & ODS_DISABLED)
  301. {
  302. DrawBottonXP(pDC,m_rect,DISABLED);
  303. }
  304. else
  305. if (m_State & ODS_FOCUS)
  306. DrawBottonXP(pDC,m_rect,FOCUS);
  307. else
  308. DrawBottonXP(pDC,m_rect,NORMAL);
  309. }
  310. else
  311. pDC->Draw3dRect(m_rect,m_clrface, m_clrface);
  312. }
  313. pDC->SetBkMode(nMode);
  314. }
  315. void CGuiButton::AutoSizeButton(BOOL m_bAutoAjust)
  316. {
  317. m_ReposWindow=m_bAutoAjust;
  318. Invalidate();
  319. UpdateWindow();
  320. }
  321. void CGuiButton::OnSysColorChange( )
  322. {
  323. CButton::OnSysColorChange( );
  324. m_clrface=GuiDrawLayer::GetRGBColorXP();
  325. }
  326. //*************************************************************************
  327. void CGuiButton::DrawBottonXP(CDC* pDC,CRect rc,int Estado)
  328. {
  329. COLORREF clrPress = ::GetSysColor (COLOR_HIGHLIGHT);
  330. COLORREF clrShadow  = ::GetSysColor(COLOR_BTNSHADOW);
  331. //*****************************************************
  332. //dibujamos los bordes del boton
  333. CPen cp(PS_SOLID,1,clrPress);
  334. CPen cpface(PS_SOLID,1,m_clrface);
  335. CPen cpsxp(PS_SOLID,1,GuiDrawLayer::GetRGBColorXP());
  336. CPen cpOver(PS_SOLID,1,RGB(255,193,111));
  337. CPen cpShadow(PS_SOLID,1,clrShadow); 
  338. CPen* pOld;
  339. if (Estado != DISABLED)
  340. pOld=pDC->SelectObject(&cp);
  341. else
  342. pOld=pDC->SelectObject(&cpShadow);
  343. rc.right-=4;
  344. rc.bottom-=2;
  345. //la linea superior
  346. pDC->MoveTo(rc.left+2,rc.top);
  347. pDC->LineTo(rc.right-1,rc.top);
  348. //linea izquierda
  349. pDC->MoveTo(rc.left,rc.top+2);
  350. pDC->LineTo(rc.left,rc.bottom-1);
  351. //linea derecha
  352. pDC->MoveTo(rc.right,rc.top+2);
  353. pDC->LineTo(rc.right,rc.bottom-1);
  354. //linea inferior
  355. pDC->MoveTo(rc.left+2,rc.bottom);
  356. pDC->LineTo(rc.right-1,rc.bottom);
  357. if (Estado != DISABLED)
  358. {
  359. //se dibuja algunas lineas para dar forma 3d
  360. //linea inferior
  361. pDC->SelectObject(&cpface);
  362. pDC->MoveTo(rc.left+2,rc.bottom-1);
  363. pDC->LineTo(rc.right-1,rc.bottom-1);
  364. pDC->MoveTo(rc.right-1,rc.top+2);
  365. pDC->LineTo(rc.right-1,rc.bottom-1);
  366. pDC->SelectObject(&cpsxp);
  367. pDC->MoveTo(rc.left+2,rc.bottom-2);
  368. pDC->LineTo(rc.right-1,rc.bottom-2);
  369. pDC->MoveTo(rc.right-2,rc.top+2);
  370. pDC->LineTo(rc.right-2,rc.bottom-1);
  371. //fin bordes del boton
  372. //***************************************************************
  373. CRect rcBordes=rc;
  374. rcBordes.left+=1;
  375. rcBordes.top+=1;
  376. switch(Estado)
  377. {
  378. case OVER:
  379. pDC->Draw3dRect(rcBordes,RGB(255,202,130),RGB(255,193,111));
  380. rcBordes.DeflateRect(1,1);
  381. pDC->Draw3dRect(rcBordes,RGB(255,202,130),RGB(255,193,111));
  382. break;
  383. case FOCUS:
  384. pDC->Draw3dRect(rcBordes,GuiDrawLayer::GetRGBFondoXP(),GuiDrawLayer::GetRGBFondoXP());
  385. rcBordes.DeflateRect(1,1);
  386. pDC->Draw3dRect(rcBordes,GuiDrawLayer::GetRGBFondoXP(),GuiDrawLayer::GetRGBFondoXP());
  387. break;
  388. }
  389. //****************************************************************
  390. //pintar los vertices
  391. //left,top
  392. pDC->SetPixel(rc.left+1,rc.top,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  393. pDC->SetPixel(rc.left,rc.top+1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  394. pDC->SetPixel(rc.left+1,rc.top+1,Estado != DISABLED?clrPress:clrShadow);
  395. //pintar los vertices
  396. //left,bottom
  397. pDC->SetPixel(rc.left+1,rc.bottom,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  398. pDC->SetPixel(rc.left,rc.bottom-1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  399. pDC->SetPixel(rc.left+1,rc.bottom-1,Estado != DISABLED?clrPress:clrShadow);
  400. //right,top
  401. pDC->SetPixel(rc.right-1,rc.top,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  402. pDC->SetPixel(rc.right,rc.top+1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  403. pDC->SetPixel(rc.right-1,rc.top+1,Estado != DISABLED?clrPress:clrShadow);
  404. //right,bottom
  405. pDC->SetPixel(rc.right-1,rc.bottom,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  406. pDC->SetPixel(rc.right,rc.bottom-1,Estado != DISABLED?GuiDrawLayer::GetRGBFondoXP():clrShadow);
  407. pDC->SetPixel(rc.right-1,rc.bottom-1,Estado != DISABLED?clrPress:clrShadow);
  408. }
  409. pDC->SelectObject(pOld);
  410. }
  411. //*****************************************************************************
  412. void CGuiButton::SetColor(COLORREF m_clrFaces)
  413. {
  414. m_clrface=m_clrFaces;
  415. }
  416. //*****************************************************************************
  417. void CGuiButton::ShowImage( BOOL m_viewimage)
  418. {
  419. m_viewImage=m_viewimage;
  420. CalLayout();
  421. }
  422. //*****************************************************************************
  423. void CGuiButton::ShowText( BOOL m_viewText)
  424. {
  425. m_ViewText=m_viewText;
  426. CalLayout();
  427. }
  428. //*****************************************************************************
  429. void CGuiButton::CalLayout()
  430. {
  431. CString m_str;
  432. CRect m_rect;
  433. CSize sizeImag=CSize(0,0);
  434. int m_High=0;
  435. if (GetSafeHwnd() == NULL) return;
  436. GetWindowText(m_str);
  437. CClientDC dc(this);
  438. ICONINFO bm;
  439. ZeroMemory(&bm, sizeof(ICONINFO));
  440. GetClientRect(&m_rect);
  441. CSize SizeCad=CSize(0,0);
  442. if (m_viewImage == TRUE)
  443. if (m_hicon[0]!= NULL)
  444. {
  445. ::GetIconInfo(m_hicon[0], &bm);
  446. sizeImag=CSize((BYTE)(bm.xHotspot * 2),(BYTE)(bm.yHotspot * 2));
  447. }
  448. if ( m_ViewText == TRUE)
  449. if (m_str.GetLength() > 0)
  450. {
  451. SizeCad=dc.GetTextExtent(m_str);
  452. SizeCad.cx-=7;
  453. }
  454. m_High=max(sizeImag.cy,SizeCad.cy);
  455. m_High=max(m_High,m_rect.Height());
  456. m_szButton= CSize(SizeCad.cx+sizeImag.cx+7,m_High);
  457. if (m_ReposWindow==TRUE)
  458. SetWindowPos (NULL, 0, 0, SizeCad.cx+sizeImag.cx+10, m_High,
  459. SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
  460. }
  461. //*****************************************************************************
  462. CSize CGuiButton::GetSizeButton()
  463. {
  464. m_ReposWindow=FALSE;
  465. CalLayout();
  466. m_ReposWindow=TRUE;
  467. return m_szButton;
  468. }
  469. //*****************************************************************************
  470. void CGuiButton::SetImage(HICON hIcon)
  471. {
  472. m_hicon[0] =hIcon;
  473. m_hicon[1] =hIcon;
  474. m_hicon[2] =hIcon;
  475. ShowImage( TRUE);
  476. }
  477. BOOL CGuiButton::SetImage(UINT uBitmapNormal,UINT uBitmapFocus,UINT uBitmapPush)
  478. {
  479. BOOL result;
  480. HINSTANCE hirc = AfxFindResourceHandle(MAKEINTRESOURCE(uBitmapNormal), RT_BITMAP);
  481. if (uBitmapNormal != -1) 
  482. {
  483. m_hicon[0] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16, 16, LR_LOADMAP3DCOLORS);
  484. result=TRUE;
  485. }
  486. else
  487. result=FALSE;
  488. if (uBitmapFocus != -1)
  489. {
  490. m_hicon[1] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapFocus), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
  491. if (m_hicon[1] == NULL && m_hicon[0]!= NULL)
  492. m_hicon[1] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
  493. }
  494. else
  495. {
  496. if (m_hicon[0]!= NULL)
  497. m_hicon[1] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
  498. }
  499. if (uBitmapPush != -1)
  500. {
  501. m_hicon[2] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapFocus), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
  502. if (m_hicon[2] == NULL && m_hicon[0]!= NULL)
  503. m_hicon[2] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS);
  504. }
  505. else
  506. {
  507. if (m_hicon[0]!= NULL)
  508. m_hicon[2] = (HICON)::LoadImage(hirc, MAKEINTRESOURCE(uBitmapNormal), IMAGE_ICON, 16,16, LR_LOADMAP3DCOLORS|LR_LOADTRANSPARENT);
  509. }
  510. CalLayout();
  511. return result;
  512. }
  513. //*****************************************************************************
  514. void CGuiButton::SetToolTip(LPCTSTR lpMsg)
  515. {
  516. CRect rc;
  517. m_lpMsg=lpMsg;
  518. if (GetSafeHwnd ()== NULL) return;
  519. if (m_lpMsg != NULL)
  520. {
  521. if (m_ToolTipCtrl.GetSafeHwnd () == NULL) 
  522. {
  523. m_ToolTipCtrl.Create(this,TTS_ALWAYSTIP);
  524. m_ToolTipCtrl.Activate(TRUE);
  525. m_ToolTipCtrl.AddTool(this,lpMsg);
  526. }
  527. else
  528. {
  529. m_ToolTipCtrl.UpdateTipText(lpMsg,this);
  530. }
  531. }
  532. else
  533. {
  534. if (m_ToolTipCtrl.GetSafeHwnd () == NULL) 
  535. {
  536. m_ToolTipCtrl.Create(this,TTS_ALWAYSTIP);
  537. m_ToolTipCtrl.AddTool(this,lpMsg);
  538. }
  539. else
  540. m_ToolTipCtrl.UpdateTipText(lpMsg,this);
  541. m_ToolTipCtrl.Activate(FALSE);
  542. }
  543. }
  544. //*****************************************************************************
  545. void CGuiButton::SetCaption(LPCSTR lpMsg)
  546. {
  547. SetWindowText(lpMsg);
  548. m_ViewText=TRUE;
  549. CalLayout();
  550. Invalidate();
  551. }
  552. //*****************************************************************************
  553. void CGuiButton::SetTypeButton(TypeButton m_TypeButton)
  554. {
  555. m_iTypeButton=m_TypeButton;
  556. if (m_iTypeButton == TYPE_ARROW)
  557. {
  558. m_iStyleArrow=ARROW_RIGHT;
  559. m_iAling=ALING_LEFT;
  560. m_iTypeButton =TYPE_ARROW;
  561. m_iStyle=STYLE_3D;
  562. m_lpMsg="";
  563. m_iSelected=0;
  564. m_viewImage=TRUE;
  565. m_ViewText=TRUE;
  566. SetAlingArrow(ARROW_RIGHT);
  567. }
  568. }
  569. //*****************************************************************************
  570. void CGuiButton::SetAlingArrow(StyleArrow m_AlingArron)
  571. {
  572. if (m_iTypeButton == TYPE_ARROW)
  573. {
  574. m_iStyleArrow=m_AlingArron;
  575. if (m_iStyleArrow==ARROW_RIGHT)
  576. {
  577. if (m_hicon[0]== NULL)
  578. m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWRIGHT);
  579. else
  580. {
  581. m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWRIGHT);
  582. m_hicon[1]=GuiDrawLayer::LoadIconLib(IDI_ARROWRIGHT);
  583. m_hicon[2]=NULL;
  584. }
  585. }
  586. else
  587. {
  588. if (m_hicon[0] == NULL)
  589. m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWDOWN);
  590. else
  591. {
  592. m_hicon[0]=GuiDrawLayer::LoadIconLib(IDI_ARROWDOWN);
  593. m_hicon[1]=GuiDrawLayer::LoadIconLib(IDI_ARROWDOWN);
  594. m_hicon[2]=NULL;
  595. }
  596. }
  597. }
  598. }
  599. //*****************************************************************************
  600. void CGuiButton::SetStyleBorder(Style m_Style)
  601. {
  602. m_iStyle=m_Style;
  603. }
  604. //*****************************************************************************
  605. void CGuiButton::SetAlingMsg(AlingMsg m_AlingMsg)
  606. {
  607. if (m_iTypeButton == TYPE_NORMAL)
  608. m_iAling=m_AlingMsg;
  609. }
  610. //*****************************************************************************
  611. int CGuiButton::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  612. {
  613. if (CButton::OnCreate(lpCreateStruct) == -1)
  614. return -1;
  615. return 0;
  616. }
  617. //*****************************************************************************
  618. BOOL CGuiButton::OnEraseBkgnd(CDC* pDC) 
  619. {
  620. return TRUE;
  621. }
  622. //*****************************************************************************
  623. void CGuiButton::OnMouseMove(UINT nFlags, CPoint point) 
  624. {
  625. // TODO: Add your message handler code here and/or call default
  626. CRect m_rect;
  627. GetClientRect(&m_rect);
  628. if (m_rect.PtInRect(point))
  629. {
  630. if (m_iSelected == 1) return;
  631. m_iSelected =1;
  632. Invalidate();
  633. UpdateWindow();
  634. SetTimer(1,10,NULL);
  635. }
  636. CButton::OnMouseMove(nFlags, point);
  637. }
  638. //*****************************************************************************
  639. void CGuiButton::PreSubclassWindow() 
  640. {
  641. ModifyStyle(0,BS_OWNERDRAW);
  642. CButton::PreSubclassWindow();
  643. }
  644. //*****************************************************************************
  645. void CGuiButton::OnLButtonUp(UINT nFlags, CPoint point) 
  646. {
  647. /*Modified By SunZhenyu 2004/08/24, rewrite this function */
  648. /* Old
  649. CButton::OnLButtonUp(nFlags, point);
  650. if (m_iSelected == 1) m_iSelected = 0;
  651. Invalidate ();
  652. UpdateWindow ();
  653. */
  654. if (m_iSelected == 1) m_iSelected = 0;
  655. Invalidate ();
  656. UpdateWindow ();
  657. CButton::OnLButtonUp(nFlags, point);
  658. }
  659. //*****************************************************************************
  660. void CGuiButton::OnLButtonDown(UINT nFlags, CPoint point) 
  661. {
  662. /*Modified By SunZhenyu 2004/08/24, rewrite this function */
  663. /* Old
  664. CButton::OnLButtonDown(nFlags, point);
  665. Invalidate();
  666. UpdateWindow();
  667. if (m_iTypeButton == TYPE_ARROW)
  668. {
  669. SetFocus ();
  670. ShowMenu();
  671. }
  672. */
  673. Invalidate();
  674. UpdateWindow();
  675. if (m_iTypeButton == TYPE_ARROW)
  676. {
  677. SetFocus ();
  678. ShowMenu();
  679. }
  680. CButton::OnLButtonDown(nFlags, point);
  681. }
  682. void CGuiButton::SetPopupMenu(CMenu* pMenu)
  683. {
  684. m_pMenu=pMenu;
  685. }
  686. //*****************************************************************************
  687. void CGuiButton::ShowMenu()
  688. {
  689. CRect rcW;
  690. int x,y;
  691. GetWindowRect(&rcW);
  692. if (m_iStyleArrow==ARROW_RIGHT)
  693. {
  694. x=rcW.right+1;
  695. y=rcW.top;
  696. }
  697. else
  698. {
  699. x=rcW.left;
  700. y=rcW.bottom;
  701. }
  702. if (m_pMenu->GetSafeHmenu() != NULL && m_iMenuActivo==FALSE)
  703. {
  704.     m_iMenuActivo=TRUE;
  705. int m_iResMenu=m_pMenu->GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, 
  706. x, y,  GetParent(), NULL);
  707. if (m_iResMenu!=0)
  708. {
  709. CWnd* pParent= GetParent();
  710. pParent->SendMessage ( WM_COMMAND,MAKEWPARAM (GetDlgCtrlID (), BN_CLICKED),(LPARAM) m_hWnd);
  711. }
  712. m_iMenuActivo=FALSE;
  713. }
  714. else
  715. return;
  716. ReleaseCapture();
  717. }
  718. //*****************************************************************************
  719. BOOL CGuiButton::PreTranslateMessage(MSG* pMsg) 
  720. {
  721. if (pMsg->message==WM_MOUSEMOVE)
  722. if (m_ToolTipCtrl.GetSafeHwnd())
  723. m_ToolTipCtrl.RelayEvent(pMsg);
  724. return CButton::PreTranslateMessage(pMsg);
  725. }
  726. //*****************************************************************************
  727. void CGuiButton::OnTimer(UINT nIDEvent) 
  728. {
  729. // TODO: Add your message handler code here and/or call default
  730. if (nIDEvent == 1)
  731. {
  732. CRect m_rect;
  733. CPoint pt(GetMessagePos());
  734. ScreenToClient(&pt);
  735. GetClientRect(m_rect);
  736. if (!m_rect.PtInRect(pt))
  737. {
  738. m_iSelected=0;
  739. Invalidate();
  740. UpdateWindow();
  741. KillTimer(1);
  742. }
  743. }
  744. CButton::OnTimer(nIDEvent);
  745. }