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

金融证券系统

开发平台:

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 "GuiHeaderCtrl.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CGuiHeaderCtrl
  22. CGuiHeaderCtrl::CGuiHeaderCtrl()
  23. {
  24. m_clrFace= ::GetSysColor(COLOR_3DFACE);
  25. m_clrShadow= ::GetSysColor(COLOR_BTNSHADOW);
  26. m_clrLight= ::GetSysColor(COLOR_BTNHIGHLIGHT);
  27. m_nNumbottomSelect=-1;
  28. m_bSentido=TRUE;
  29. }
  30. CGuiHeaderCtrl::~CGuiHeaderCtrl()
  31. {
  32. }
  33. BEGIN_MESSAGE_MAP(CGuiHeaderCtrl, CHeaderCtrl)
  34. //{{AFX_MSG_MAP(CGuiHeaderCtrl)
  35. ON_WM_CREATE()
  36. ON_WM_PAINT()
  37. ON_WM_WINDOWPOSCHANGED()
  38. ON_WM_ERASEBKGND()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CGuiHeaderCtrl message handlers
  43. //***************************************************************************
  44. void CGuiHeaderCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  45. {
  46. CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  47. CRect rcitem = lpDrawItemStruct->rcItem;
  48. UINT uState = lpDrawItemStruct->itemState;
  49. int     nItemID =   lpDrawItemStruct->itemID;
  50. CBrush m_brush;
  51. CString m_caption;
  52. CSize siImg=CSize(0,0);
  53. TCHAR caption[256];
  54. IMAGEINFO pImageInfo;
  55. int OldMode=pDC->SetBkMode(TRANSPARENT);
  56. m_brush.CreateSysColorBrush(COLOR_3DFACE);
  57. pDC->FillRect(rcitem,&m_brush);
  58. HDITEM hdi;
  59. hdi.mask = HDI_TEXT | HDI_FORMAT|HDI_IMAGE| HDI_ORDER;
  60. hdi.pszText = caption;
  61. hdi.cchTextMax = 255;
  62. GetItem( nItemID, &hdi );
  63. //---------------------------------------------------------
  64. //se mueve la imagen para dar la sensacion
  65. //de seleccion del item
  66. //---------------------------------------------------------
  67. if (uState == ODS_SELECTED) 
  68. {
  69. rcitem.left+=1;
  70. rcitem.top+=1;
  71. }
  72. if (m_nNumbottomSelect==nItemID)
  73. {
  74. DrawArrow(pDC,rcitem,m_bSentido);
  75. rcitem.right-=12;
  76. }
  77. if (hdi.iImage > -1)
  78. {
  79. m_img.GetImageInfo(hdi.iImage,&pImageInfo);
  80. CRect rcima =pImageInfo.rcImage;
  81. siImg=CSize(rcima.Width(),rcima.Height());
  82. CPoint pti(rcitem.left+2,rcitem.top+1);
  83. m_img.Draw(pDC, hdi.iImage, pti, ILD_TRANSPARENT);
  84. }
  85. if (strlen(caption) > 0)
  86. {
  87. CRect m_rctext=rcitem;
  88. m_rctext.left+=siImg.cx+4;
  89. m_rctext.bottom-=2;
  90. DrawTextExt(caption,pDC,&m_rctext,DT_SINGLELINE|DT_BOTTOM);
  91. }
  92. pDC->SetBkMode(OldMode);
  93. }
  94. //**********************************************************************************
  95. void CGuiHeaderCtrl::DrawTextExt(CString Cad,CDC* pDC,CRect* rc,UINT nFlags)
  96. {
  97. CString m_cadBreak=Cad;
  98. int c=0;
  99. int cont=m_cadBreak.GetLength();
  100. CSize coor=pDC->GetTextExtent(m_cadBreak,m_cadBreak.GetLength());
  101. if (coor.cx > rc->Width()-1)
  102. {
  103. while(cont > 1 )
  104. {
  105. CString m_scadtemp=m_cadBreak+"...";
  106. coor=pDC->GetTextExtent(m_scadtemp,m_scadtemp.GetLength());
  107. if(coor.cx > rc->Width())
  108. m_cadBreak=m_cadBreak.Left(m_cadBreak.GetLength()-1);
  109. else
  110. break;
  111. cont--;
  112. }
  113. m_cadBreak+=_T("...");
  114. rc->right+=3;
  115. }
  116. pDC->DrawText(m_cadBreak,rc,nFlags);
  117. }
  118. //********************************************************************************
  119. void CGuiHeaderCtrl::DrawArrow(CDC* pDC,CRect rc,BOOL bUp)
  120. {
  121. CPen cp(PS_SOLID,1, m_clrFace);
  122. CPen cps(PS_SOLID,1, m_clrShadow);
  123. CPen cpw(PS_SOLID,1, m_clrLight);
  124. CPen *pOld;
  125. rc.left=rc.right-12;
  126. rc.right=rc.left+8;
  127. rc.bottom=rc.top+12;
  128. rc.top+=2;
  129. int m_mitad=rc.left+4;
  130. if (bUp == TRUE)
  131. {
  132. //linea izquierda
  133. pOld=pDC->SelectObject(&cps);
  134. pDC->MoveTo(rc.left,rc.bottom);
  135. pDC->LineTo(m_mitad,rc.top);
  136. //linea derecha
  137. pDC->SelectObject(&cpw);
  138. pDC->MoveTo(rc.right,rc.bottom);
  139. pDC->LineTo(m_mitad,rc.top);
  140. //linea de abajo
  141. pDC->MoveTo(rc.left,rc.bottom);
  142. pDC->LineTo(rc.right,rc.bottom);
  143. }
  144. else
  145. {
  146. rc.bottom=rc.top+12;
  147. rc.top+=4;
  148. //linea izquierda
  149. pOld=pDC->SelectObject(&cps);
  150. pDC->MoveTo(rc.left,rc.top);
  151. pDC->LineTo(m_mitad,rc.bottom);
  152. //linea superior
  153. pDC->MoveTo(rc.left,rc.top);
  154. pDC->LineTo(rc.right,rc.top);
  155. //linea derecha
  156. pDC->SelectObject(&cpw);
  157. pDC->MoveTo(rc.right,rc.top);
  158. pDC->LineTo(m_mitad,rc.bottom);
  159. }
  160. pDC->SelectObject(pOld);
  161. }
  162. //*******************************************************************
  163. void CGuiHeaderCtrl::DelImageCol(int icol,int img)
  164. {
  165. HDITEM hdi;
  166. GetItem(icol,&hdi);
  167. hdi.iImage=-1;
  168. hdi.fmt |=  HDF_IMAGE;
  169. hdi.mask |= HDI_IMAGE | HDI_FORMAT;
  170. SetItem(icol,&hdi);
  171. }
  172. //*********************************************************************
  173. void CGuiHeaderCtrl::SetImageCol(int icol,int img)
  174. {
  175. HDITEM hdi;
  176. GetItem(icol,&hdi);
  177. hdi.iImage=img;
  178. hdi.fmt |=  HDF_IMAGE;
  179. hdi.mask |= HDI_IMAGE | HDI_FORMAT;
  180. SetItem(icol,&hdi);
  181. }
  182. //********************************************************************
  183. int CGuiHeaderCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  184. {
  185. if (CHeaderCtrl::OnCreate(lpCreateStruct) == -1)
  186. return -1;
  187. m_pParentWnd = CWnd::FromHandle(lpCreateStruct->hwndParent);
  188. ASSERT(m_pParentWnd);
  189. return 0;
  190. }
  191. //********************************************************************
  192. void CGuiHeaderCtrl::PreSubclassWindow() 
  193. {
  194. m_pParentWnd = GetParent();
  195. ASSERT(m_pParentWnd);
  196. CHeaderCtrl::PreSubclassWindow();
  197. ClearImages();
  198. ReDraw(); 
  199. }
  200. //********************************************************************
  201. void CGuiHeaderCtrl::ReDraw() 
  202. {
  203. int iNumItems=GetItemCount();
  204. HDITEM m_hditems;
  205. m_hditems.fmt  |= HDF_OWNERDRAW;
  206. for (int i =0; i < iNumItems; i++)
  207. SetItem(i,&m_hditems);
  208. }
  209. //*********************************************************************
  210. void CGuiHeaderCtrl::ClearImages()
  211. {
  212. HDITEM m_hditems;
  213. m_hditems.mask |=HDI_FORMAT|HDI_IMAGE;
  214. int iNumItems=GetItemCount();
  215. for (int i =0; i < iNumItems; i++)
  216. {
  217. m_hditems.iImage=-1;
  218. SetItem(i,&m_hditems);
  219. }
  220. }
  221. //********************************************************************
  222. void CGuiHeaderCtrl::OnPaint() 
  223. {
  224. CPaintDC dc(this); // device context for painting
  225. CDC MemDC;
  226. CBitmap m_BitmapMemDC;
  227. CRect rcClient;
  228. CRect rcWin;
  229. CBrush m_brush;
  230. //******************************************
  231. GetWindowRect( &rcWin);
  232. GetClientRect( &rcClient );
  233. MemDC.CreateCompatibleDC(&dc);
  234. m_BitmapMemDC.CreateCompatibleBitmap(&dc,
  235.   rcClient.Width(),rcClient.Height());
  236. CBitmap* m_bitmapOld=MemDC.SelectObject(&m_BitmapMemDC);
  237. m_brush.CreateSysColorBrush(COLOR_3DFACE);
  238. MemDC.FillRect(rcClient,&m_brush); //evita el horrible color negro
  239. //*****************************************************
  240. CWnd::DefWindowProc( WM_PAINT, (WPARAM)MemDC.m_hDC, 0 );
  241. //*****************************************************
  242. ScreenToClient(rcWin);
  243. BiselaBoton(rcWin,&MemDC);
  244. dc.BitBlt(0,0,rcClient.Width(),rcClient.Height(),&MemDC,
  245.       rcClient.left,rcClient.top,SRCCOPY);
  246. MemDC.SelectObject(m_bitmapOld);
  247. m_BitmapMemDC.DeleteObject();
  248. MemDC.DeleteDC();
  249. }
  250. //************************************************************************
  251. void CGuiHeaderCtrl::BiselaBoton(CRect rcWin,CDC* pDC)
  252. {
  253. CPen cp(PS_SOLID,1, m_clrFace);
  254. CPen cps(PS_SOLID,1, m_clrShadow);
  255. CPen cpw(PS_SOLID,1, m_clrLight);
  256. //***************************************************
  257. pDC->Draw3dRect(rcWin,m_clrLight,m_clrShadow);
  258. rcWin.DeflateRect(1,1);
  259. pDC->Draw3dRect(rcWin,m_clrFace,m_clrFace);
  260. //***************************************************
  261. CPen* cpold=pDC->SelectObject(&cp);
  262. int iNumItems=GetItemCount();
  263. int iContx=0;
  264. HDITEM m_hditems;
  265. for (int i =0; i < iNumItems; i++)
  266. {
  267. CRect recItem;
  268. GetItem(i,&m_hditems);
  269. GetItemRect(i, recItem);
  270. iContx+=recItem.Width();
  271. //quitamos todas las lineas
  272. recItem.DeflateRect(1,1);
  273. pDC->SelectObject(&cp);
  274. pDC->MoveTo(iContx-2,rcWin.top+1);
  275. pDC->LineTo(iContx-2,rcWin.bottom-1);
  276. pDC->MoveTo(iContx-1,rcWin.top+1);
  277. pDC->LineTo(iContx-1,rcWin.bottom-1);
  278. pDC->MoveTo(iContx,rcWin.top+1);
  279. pDC->LineTo(iContx,rcWin.bottom-1);
  280. pDC->MoveTo(iContx+1,rcWin.top+1);
  281. pDC->LineTo(iContx+1,rcWin.bottom-1);
  282. //ponemos dos para dar el efecto
  283. pDC->SelectObject(&cps);
  284. pDC->MoveTo(iContx-1,rcWin.top+2);
  285. pDC->LineTo(iContx-1,rcWin.bottom-2);
  286. pDC->SelectObject(&cpw);
  287. pDC->MoveTo(iContx,rcWin.top+2);
  288. pDC->LineTo(iContx,rcWin.bottom-2);
  289. }
  290. pDC->SelectObject(cpold);
  291. }
  292. //*********************************************************************
  293. void CGuiHeaderCtrl::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
  294. {
  295. CHeaderCtrl::OnWindowPosChanged(lpwndpos);
  296. Invalidate();
  297. UpdateWindow();
  298. ReDraw();
  299. }
  300. //*********************************************************************************
  301. BOOL CGuiHeaderCtrl::OnEraseBkgnd(CDC* pDC) 
  302. {
  303. return TRUE;
  304. }
  305. //*********************************************************************************
  306. void CGuiHeaderCtrl::SetImageList(UINT nBitmapID, int cx, int nGrow, COLORREF crMask)
  307. {
  308. CBitmap cbmp;
  309. BITMAP bmp;
  310. m_img.Create(nBitmapID,cx,nGrow,crMask);
  311. cbmp.LoadBitmap(nBitmapID);
  312. cbmp.GetBitmap(&bmp);
  313. m_img.Add(&cbmp,crMask);
  314. m_sizeImag=CSize(cx,bmp.bmHeight);
  315. }
  316. int CGuiHeaderCtrl::GetSortColumn(BOOL* bSortCol )
  317. {
  318. *bSortCol =m_bSentido;
  319. return m_nNumbottomSelect;
  320. }
  321. void CGuiHeaderCtrl::SetSortColumn(int nCol,BOOL sentido)
  322. {
  323. m_nNumbottomSelect =nCol;
  324. m_bSentido=sentido;
  325. Invalidate();
  326. UpdateWindow();
  327. }