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

对话框与窗口

开发平台:

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