SkinHeaderCtrl.cpp
上传用户:sgmlaoniu
上传日期:2022-06-15
资源大小:28k
文件大小:4k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // SkinHeaderCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SkinList.h"
  5. #include "SkinHeaderCtrl.h"
  6. #include "memdc.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CSkinHeaderCtrl
  14. CSkinHeaderCtrl::CSkinHeaderCtrl()
  15. {
  16. }
  17. CSkinHeaderCtrl::~CSkinHeaderCtrl()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CSkinHeaderCtrl, CHeaderCtrl)
  21. //{{AFX_MSG_MAP(CSkinHeaderCtrl)
  22. ON_WM_PAINT()
  23. ON_WM_ERASEBKGND()
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CSkinHeaderCtrl message handlers
  28. void CSkinHeaderCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  29. {
  30. }
  31. void CSkinHeaderCtrl::OnPaint() 
  32. {
  33. CPaintDC dc(this); // device context for painting
  34. CRect rect, rectItem, clientRect;
  35. GetClientRect(&rect);
  36. GetClientRect(&clientRect);
  37. CMemDC memDC(&dc, rect);
  38. CDC bitmapDC;
  39. bitmapDC.CreateCompatibleDC(&dc);
  40. memDC.FillSolidRect(&rect, RGB(76,85,118));
  41. CBitmap bitmapSpan;
  42. bitmapSpan.LoadBitmap(IDB_COLUMNHEADER_SPAN);
  43. CBitmap* pOldBitmapSpan = bitmapDC.SelectObject(&bitmapSpan);
  44. memDC.StretchBlt(rect.left+2, 0, rect.Width(), 12, &bitmapDC, 0, 0, 1, 12, SRCCOPY);
  45. bitmapDC.SelectObject(pOldBitmapSpan);
  46. bitmapSpan.DeleteObject();
  47. int nItems = GetItemCount();
  48. CBitmap bitmap;
  49. CBitmap bitmap2;
  50. CBitmap bitmap3;
  51. bitmap.LoadBitmap(IDB_COLUMNHEADER_START);
  52. bitmap2.LoadBitmap(IDB_COLUMNHEADER_SPAN);
  53. bitmap3.LoadBitmap(IDB_COLUMNHEADER_END);
  54. for(int i = 0; i <nItems; i++)
  55. {
  56. TCHAR buf1[256];
  57. HD_ITEM hditem1;
  58. hditem1.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
  59. hditem1.pszText = buf1;
  60. hditem1.cchTextMax = 255;
  61. GetItem( i, &hditem1 );
  62. GetItemRect(i, &rect);
  63. CBitmap* pOldBitmap = NULL;
  64. //make sure we draw the start piece
  65. //on the first item so it has a left border
  66. //For the following items we will just use the
  67. //right border of the previous items as the left
  68. //border
  69. if(hditem1.iOrder==0)
  70. {
  71. pOldBitmap = bitmapDC.SelectObject(&bitmap);
  72. memDC.BitBlt(rect.left,rect.top,2,12,&bitmapDC,0,0,SRCCOPY);
  73. }
  74. else
  75. {
  76. memDC.BitBlt(rect.left-1,rect.top,2,12,&bitmapDC,0,0,SRCCOPY);
  77. pOldBitmap = bitmapDC.SelectObject(&bitmap2);
  78. memDC.BitBlt(rect.left+1,rect.top,1,12,&bitmapDC,0,0,SRCCOPY);
  79. }
  80. bitmapDC.SelectObject(pOldBitmap);
  81. //span the bitmap for the width of the column header item
  82. int nWidth = rect.Width() - 4;
  83. CBitmap* pOldBitmap2 = bitmapDC.SelectObject(&bitmap2);
  84. memDC.StretchBlt(rect.left+2, 0, nWidth, 1, &bitmapDC, 0,0, 1, 12, SRCCOPY);
  85. bitmapDC.SelectObject(pOldBitmap2);
  86. //draw the end piece of the column header
  87. CBitmap* pOldBitmap3 = bitmapDC.SelectObject(&bitmap3);
  88. memDC.BitBlt((rect.right-2), 0, 2, 12, &bitmapDC,0,0,SRCCOPY);
  89. bitmapDC.SelectObject(pOldBitmap3);
  90. //Get all the info for the current
  91. //item so we can draw the text to it
  92. //in the desired font and style
  93. DRAWITEMSTRUCT DrawItemStruct;
  94. GetItemRect(i, &rectItem);
  95. DrawItemStruct.CtlType = 100;
  96. DrawItemStruct.hDC = dc.GetSafeHdc();
  97. DrawItemStruct.itemAction = ODA_DRAWENTIRE; 
  98. DrawItemStruct.hwndItem  = GetSafeHwnd(); 
  99. DrawItemStruct.rcItem = rectItem;
  100. DrawItemStruct.itemID = i;
  101. DrawItem(&DrawItemStruct);
  102. UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_CENTER | DT_END_ELLIPSIS ;
  103. CFont font;
  104. LOGFONT lf;
  105. memset(&lf, 0, sizeof(LOGFONT));
  106. lf.lfHeight = 8;
  107. strcpy(lf.lfFaceName, "Sevenet 7");
  108. font.CreateFontIndirect(&lf);
  109. CFont* def_font = memDC.SelectObject(&font);
  110. memDC.SetBkMode(TRANSPARENT);
  111. rectItem.DeflateRect(2,2,2,2);
  112. TCHAR buf[256];
  113. HD_ITEM hditem;
  114. hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
  115. hditem.pszText = buf;
  116. hditem.cchTextMax = 255;
  117. GetItem( DrawItemStruct.itemID, &hditem );
  118. memDC.DrawText(buf, &rectItem, uFormat);
  119. memDC.SelectObject(def_font);
  120. font.DeleteObject();
  121. }
  122. }
  123. BOOL CSkinHeaderCtrl::OnEraseBkgnd(CDC* pDC) 
  124. {
  125. return false;
  126. }