GuiMiniTool.cpp
上传用户:wlkj888
上传日期:2022-08-01
资源大小:806k
文件大小:12k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. /****************************************************************************
  2.  * *  
  3.  * GuiToolKit   *
  4.  *  (MFC extension) *  
  5.  * Created by Francisco Campos G. www.beyondata.com fcampos@beyondata.com *
  6.  *--------------------------------------------------------------------------*    
  7.  * *
  8.  * This program is free software;so you are free to use it any of your *
  9.  * applications (Freeware, Shareware, Commercial),but leave this header *
  10.  * intact. *
  11.  * *
  12.  * These files are provided "as is" without warranty of any kind. *
  13.  * *
  14.  *        GuiToolKit is forever FREE CODE !!!!! *
  15.  * *
  16.  *--------------------------------------------------------------------------*
  17.  * Created by: Francisco Campos G. *
  18.  * Bug Fixes and improvements : (Add your name) *
  19.  * -Francisco Campos *
  20.  * -Ernesto Garcia *
  21.  ****************************************************************************/
  22. #include "stdafx.h"
  23. #include "GuiMiniTool.h"
  24. #include "GuiDrawLayer.h"
  25. #include "GuiToolButton.h"
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CGuiMiniTool
  33. CGuiMiniTool::CGuiMiniTool()
  34. {
  35. m_arrButtons.RemoveAll();
  36. nNumItems=0;
  37. m_AlingButton=ALIGN_LEFT;
  38. m_AlinTexto=ALIGN_LEFT;
  39. m_clrface=GuiDrawLayer::GetRGBColorXP();
  40. m_bAutoSize=FALSE;
  41. m_bExisteCombo=FALSE;
  42. m_cfont.CreateFont(-11,0,0,0,400,0,0,0,0,1,2,1,34,_T("MS Sans Serif"));
  43. m_clrCaption=GuiDrawLayer::GetRGBColorShadow(GuiDrawLayer::m_Style);
  44. m_Caption=_T("");
  45. msz=CSize(0,0);
  46. m_StyleDisplay=GUISTYLE_XP;
  47. }
  48. CGuiMiniTool::~CGuiMiniTool()
  49. {
  50. m_arrButtons.RemoveAll();
  51. }
  52. BEGIN_MESSAGE_MAP(CGuiMiniTool, CStatic)
  53. //{{AFX_MSG_MAP(CGuiMiniTool)
  54. ON_WM_CREATE()
  55. ON_WM_SIZE()
  56. ON_WM_ERASEBKGND()
  57. ON_WM_PAINT()
  58. ON_WM_SYSCOLORCHANGE()
  59. //}}AFX_MSG_MAP
  60. ON_WM_DESTROY()
  61. END_MESSAGE_MAP()
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CGuiMiniTool message handlers
  64. int CGuiMiniTool::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  65. {
  66. if (CStatic::OnCreate(lpCreateStruct) == -1)
  67. return -1;
  68. // TODO: Add your specialized creation code here
  69. return 0;
  70. }
  71. void CGuiMiniTool::OnSysColorChange( )
  72. {
  73. CStatic::OnSysColorChange( );
  74. StyleDispl(m_StyleDisplay);
  75. }
  76. //********************************************************************************
  77. void  CGuiMiniTool::PreSubclassWindow()
  78. {
  79. CStatic::PreSubclassWindow();
  80. }
  81. //********************************************************************************
  82. void  CGuiMiniTool::AddButton(int nIndex,UINT Cmd,CMenu* pMenu,CString m_szCaption,CString m_ToolTip)
  83. {
  84. m_arrButtons.SetAtGrow(nNumItems, new CArrButton
  85. (this,m_imgList.ExtractIcon(nIndex),Cmd,&m_cfont,m_szCaption,pMenu,m_ToolTip));
  86. nNumItems++;
  87. RecalLayout();
  88. }
  89. //**************************************************************************
  90. HICON CGuiMiniTool::GetImagIcon(int nNum)
  91. {
  92. return m_imgList.ExtractIcon(nNum);
  93. }
  94. //****************************************************************************
  95. BOOL  CGuiMiniTool::CreateCombo(CGuiComboBoxExt* pControl,UINT nID,int iSize,
  96. DWORD dwStyle)
  97. {
  98. ASSERT(pControl);
  99. if (!pControl->Create(dwStyle, 
  100.   CRect(1,1,iSize,250), this, nID))
  101.    return FALSE;
  102. pControl->m_IsCallMiniTool=TRUE;
  103. m_arrButtons.SetAtGrow(nNumItems, new CArrButton(pControl));
  104. nNumItems++;
  105. RecalLayout();
  106. return TRUE;
  107. }
  108. //********************************************************************************
  109. void  CGuiMiniTool::SetImageList(UINT nBitmapID, int cx, int nGrow, COLORREF crMask)
  110. {
  111. CBitmap cbmp;
  112. BITMAP bmp;
  113. m_imgList.Create(nBitmapID,cx,nGrow,crMask);
  114. cbmp.LoadBitmap(nBitmapID);
  115. cbmp.GetBitmap(&bmp);
  116. m_imgList.Add(&cbmp,crMask);
  117. }
  118. //********************************************************************************
  119. void CGuiMiniTool::AutoSize(BOOL mbAutosize)
  120. {  
  121. m_bAutoSize=mbAutosize;
  122. RecalLayout();
  123. }
  124. //********************************************************************************
  125. void  CGuiMiniTool::DelButton(int nIndex)
  126. {
  127. nNumItems--;
  128. m_arrButtons.RemoveAt(nIndex,nNumItems);
  129. RecalLayout();
  130. }
  131. //********************************************************************************
  132. int   CGuiMiniTool::GetCountButton()
  133. {
  134. return m_arrButtons.GetSize();
  135. }
  136. //********************************************************************************
  137. void  CGuiMiniTool::RecalLayout()
  138. {
  139. if (!GetSafeHwnd()) return;
  140. CRect rcClient;
  141. GetClientRect(rcClient);
  142. rcClient.DeflateRect(1,1);
  143. CRect rctemp=rcClient;
  144. int nTipo=0;
  145. if (m_bAutoSize)
  146. CalcAutoSize(rcClient);
  147. if (msz.cx > 0)
  148. {
  149. if (m_AlinTexto==ALIGN_LEFT)
  150. rctemp.left+=msz.cx;
  151. else
  152. rctemp.right-=msz.cx;
  153. }
  154. for (int i=0; i < nNumItems;i++)
  155. {
  156. CArrButton* m_cwnd=(CArrButton*) m_arrButtons [i];
  157. if(m_cwnd->m_btipo== TRUE)  //es un boton
  158. {
  159. CSize sztemp=((CArrButton*) m_arrButtons [i])->m_bt.GetSizeButton();
  160. nTipo=sztemp.cx;
  161. }
  162. else
  163. {
  164. CRect rctemp;
  165. m_cwnd->m_pwnd->GetWindowRect(rctemp);
  166. if (m_bAutoSize)
  167. {
  168. nTipo=m_nResultCombo;
  169. rctemp.right=rctemp.left+nTipo;
  170. }
  171. else
  172. nTipo=rctemp.Width();
  173. }
  174. if (m_AlingButton == ALIGN_RIGHT)
  175. rctemp.left=rctemp.right-nTipo;
  176. else
  177. rctemp.right=rctemp.left+nTipo;
  178. if (m_cwnd->m_btipo==TRUE)
  179. m_cwnd->m_bt.MoveWindow(rctemp);
  180. else
  181. m_cwnd->m_pwnd->MoveWindow(rctemp);
  182. if (m_AlingButton == ALIGN_RIGHT)
  183. rctemp.right-=nTipo;
  184. else
  185. rctemp.left+=nTipo;
  186. }
  187. m_rcDrawTexto=rctemp;
  188. Drawtext();
  189. }
  190. //*********************************************************************
  191. void CGuiMiniTool::AlingButtons(Aling Alingsb)
  192. {
  193. m_AlingButton=Alingsb;
  194. }
  195. void CGuiMiniTool::SetCaption(CString mSzCaption,Aling AlinText)
  196. {
  197. CClientDC dc(this);
  198. m_Caption=mSzCaption;
  199. msz=dc.GetTextExtent(m_Caption,m_Caption.GetLength());
  200. m_AlinTexto=AlinText;
  201. }
  202. void CGuiMiniTool::SetColorCaption(COLORREF clrCaption)
  203. {
  204. m_clrCaption=clrCaption;
  205. }
  206. //*********************************************************************
  207. void CGuiMiniTool::CalcAutoSize(CRect m_rc)
  208. {
  209. m_nResultCombo=0;
  210.     m_nResultButtons=0; 
  211. int m_numCombo=0;
  212. for (int i=0; i < nNumItems;i++)
  213. {
  214. if (((CArrButton*) m_arrButtons [i])->m_btipo ==TRUE)
  215. {
  216. CArrButton* mt=(CArrButton*) m_arrButtons [i];
  217. CSize sztemp=((CArrButton*) m_arrButtons [i])->m_bt.GetSizeButton();
  218. m_nResultButtons+=sztemp.cx;
  219. }
  220. else
  221. {
  222. m_bExisteCombo=TRUE;
  223. m_numCombo++;
  224. }
  225. }
  226. //calculamos el espacio para los combobox
  227. if (m_bExisteCombo)
  228. {
  229. if (m_nResultButtons+msz.cx > m_rc.Width())  //el espacio no alcanza para los botones
  230. m_nResultCombo=0;
  231. else
  232. {
  233. if ((GetSystemMetrics(SM_CXHTHUMB)+msz.cx) > m_rc.Width()-50)
  234. m_nResultCombo=0;
  235. else
  236. {
  237. m_nResultCombo=m_rc.Width()-(m_nResultButtons+msz.cx);
  238. m_nResultCombo/=m_numCombo;
  239. }
  240. }
  241. }
  242. }
  243. //********************************************************************************
  244. void CGuiMiniTool::SetBorder(Borders Style)
  245. {
  246. m_border=Style;
  247. }  
  248. //********************************************************************************
  249. void CGuiMiniTool::OnPaint() 
  250. {
  251. CPaintDC dc(this); // device context for painting
  252. CBrush cbr;
  253. CRect rcClient;
  254. GetClientRect(&rcClient);
  255. cbr.CreateSolidBrush(m_clrface);
  256. CDC MemDC;
  257. CBitmap m_BitmapMemDC;
  258. MemDC.CreateCompatibleDC(&dc);
  259. m_BitmapMemDC.CreateCompatibleBitmap(&dc,
  260.   rcClient.Width(),rcClient.Height());
  261. CBitmap* m_bitmapOld=MemDC.SelectObject(&m_BitmapMemDC);
  262. if(m_StyleDisplay==GUISTYLE_XP)
  263. MemDC.FillRect(&rcClient,&cbr);
  264. if(m_StyleDisplay== GUISTYLE_2003)
  265. {
  266. CGradient M(CSize(rcClient.Width(),rcClient.Height()+1));
  267. M.PrepareVertical(&MemDC,GUISTYLE_2003);
  268. M.Draw(&MemDC,0,0,0,0,rcClient.Width(),rcClient.Height(),SRCCOPY);
  269. }
  270. if (m_border == STYLE3D)
  271. MemDC.Draw3dRect(rcClient,GuiDrawLayer::GetRGBColorBTNHigh(),
  272. GuiDrawLayer::GetRGBColorShadow(GuiDrawLayer::m_Style));
  273. if (m_border == STYLEPRESS)
  274. {
  275. MemDC.Draw3dRect(rcClient,GuiDrawLayer::GetRGBColorShadow(GuiDrawLayer::m_Style),
  276. GuiDrawLayer::GetRGBColorBTNHigh());
  277. }
  278. if (m_border == STYLEFRAME)
  279. {
  280. MemDC.Draw3dRect(rcClient,GuiDrawLayer::GetRGBColorShadow(GuiDrawLayer::m_Style),
  281. GuiDrawLayer::GetRGBColorShadow(GuiDrawLayer::m_Style));
  282. }
  283. dc.BitBlt(0,0,rcClient.Width(),rcClient.Height(),&MemDC,
  284.       rcClient.left,rcClient.top,SRCCOPY);
  285. MemDC.SelectObject(m_bitmapOld);
  286. m_BitmapMemDC.DeleteObject();
  287. MemDC.DeleteDC();
  288. Drawtext();
  289. cbr.DeleteObject();
  290. /*
  291. CPaintDC dc(this); // device context for painting
  292. CBrush cbr;
  293. CRect m_rectDraw;
  294. GetClientRect(&m_rectDraw);
  295. cbr.CreateSolidBrush(m_clrface);
  296. dc.FillRect(&m_rectDraw,&cbr);
  297. */ //el flat ya esta
  298. }
  299. //********************************************************************************
  300. void CGuiMiniTool::SetColor(COLORREF m_clrFace)
  301. {
  302. m_clrface=m_clrFace;
  303. for (int i=0; i < nNumItems;i++)
  304.      if (((CArrButton*) m_arrButtons [i])->m_btipo ==TRUE)
  305. ((CArrButton*) m_arrButtons [i])->m_bt.SetColor(m_clrface);
  306. else
  307. ((CArrButton*) m_arrButtons [i])->m_pwnd->SetColor(m_clrface);
  308. }
  309. //********************************************************************************
  310. void CGuiMiniTool::OnSize(UINT nType, int cx, int cy) 
  311. {
  312. CStatic::OnSize(nType, cx, cy);
  313. RecalLayout();
  314. // TODO: Add your message handler code here
  315. }
  316. //********************************************************************************
  317. BOOL CGuiMiniTool::OnEraseBkgnd(CDC* pDC) 
  318. {
  319. // TODO: Add your message handler code here and/or call default
  320. return TRUE;
  321. }
  322. //********************************************************************************
  323. CArrButton::CArrButton(CGuiMiniTool* pParent,HICON m_hIcon,UINT Cmd,CFont* m_cfont,CString m_szCaption,
  324.    CMenu *pMenu,CString m_ToolTip)
  325. {
  326. m_bt.Create(_T(""), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_OWNERDRAW, 
  327. CRect(0,0,0,0), pParent, Cmd);
  328. if (!m_szCaption.IsEmpty())
  329. {
  330. m_bt.SetFont(m_cfont);
  331. m_bt.SetCaption(m_szCaption);
  332. }
  333. // m_bt=m_button;
  334. m_bt.SethIcon(m_hIcon);
  335. m_bt.SetToolTip(m_ToolTip);
  336. m_bt.SetMenu(pMenu);
  337. m_btipo=TRUE;
  338. }
  339. //********************************************************************************
  340. CArrButton::CArrButton(CGuiComboBoxExt* m_pComponent)
  341. {
  342. ASSERT(m_pComponent);
  343. m_pwnd=m_pComponent;
  344. m_btipo=FALSE;
  345. }
  346. //********************************************************************************
  347. BOOL CGuiMiniTool::OnCommand(WPARAM wParam, LPARAM lParam)
  348. {
  349. HWND hwnd = (HWND) lParam;
  350. for (int i=0; i < nNumItems;i++)
  351. {
  352. if (((CArrButton*) m_arrButtons [i])->m_btipo ==TRUE)
  353. if (((CArrButton*) m_arrButtons [i])->m_bt.m_hWnd
  354. == hwnd)
  355. {
  356. int x=((CArrButton*) m_arrButtons [i])->m_bt.GetDlgCtrlID();
  357. CWnd* pParent= GetParent();
  358. pParent->SendMessage (WM_COMMAND,x);
  359. AfxGetMainWnd()->SendMessage(WM_COMMAND,x);
  360. return TRUE;
  361. }
  362. }
  363. return FALSE;
  364. }
  365. CGuiToolButton*  CGuiMiniTool::GetButton(UINT uID_Object)//,CGuiToolButton* m_btn)
  366. {
  367. for (int i=0; i < nNumItems;i++)
  368. {
  369. if (((CArrButton*) m_arrButtons [i])->m_btipo ==TRUE)
  370. if (((CArrButton*) m_arrButtons [i])->m_bt.GetDlgCtrlID()==(int signed )uID_Object)
  371. {
  372. CGuiToolButton* m_btn=&((CArrButton*) m_arrButtons [i])->m_bt;
  373. return m_btn;
  374. }
  375. }
  376. return NULL;//m_btn=NULL;
  377. }
  378. void CGuiMiniTool::Drawtext()
  379. {
  380. CClientDC dc(this);
  381. CRect rc;
  382. GetClientRect(rc);
  383. CFont* m_fontOld=dc.SelectObject(&m_cfont);
  384. int nMode = dc.SetBkMode(TRANSPARENT);
  385. dc.SetTextColor(m_clrCaption);
  386. if(m_AlinTexto==ALIGN_LEFT)
  387. {
  388. rc.left+=5;
  389. dc.DrawText(m_Caption,rc,DT_SINGLELINE|DT_LEFT|DT_VCENTER) ;
  390. }
  391. else
  392. {
  393. m_rcDrawTexto.left=(long)(rc.right-msz.cx*1.1);
  394. m_rcDrawTexto.right=m_rcDrawTexto.left+msz.cx;
  395. dc.DrawText(m_Caption,m_rcDrawTexto,DT_SINGLELINE|DT_RIGHT|DT_VCENTER) ;
  396. }
  397. dc.SetBkMode(nMode);
  398. dc.SelectObject(m_fontOld);
  399. }
  400. //thanks to  Ernesto Garcia
  401. void CGuiMiniTool::OnDestroy()
  402. {
  403. for( int i=0; i<nNumItems; i++ )
  404.     {
  405.     CArrButton *pArr = (CArrButton*)m_arrButtons.GetAt(i);
  406.     if( pArr )
  407.         delete pArr;
  408.     }
  409. CStatic::OnDestroy();
  410. // TODO: Add your message handler code here
  411. }