自绘CListCtrl类,重载虚函数DrawItem

时间:2023-03-10 01:48:35
自绘CListCtrl类,重载虚函数DrawItem
  1. //自绘CListCtrl类,重载虚函数DrawItem
  2. void CNewListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  3. {
  4. // TODO: Add your code to draw the specified item
  5. ASSERT(lpDrawItemStruct->CtlType == ODT_LISTVIEW);
  6. CDC dc;
  7. dc.Attach(lpDrawItemStruct->hDC);
  8. ASSERT(NULL != dc.GetSafeHdc());
  9. // Save these value to restore them when done drawing.
  10. COLORREF crOldTextColor = dc.GetTextColor();
  11. COLORREF crOldBkColor = dc.GetBkColor();
  12. // If this item is selected, set the background color
  13. // and the text color to appropriate values. Also, erase
  14. // rect by filling it with the background color.
  15. if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
  16. (lpDrawItemStruct->itemState & ODS_SELECTED))
  17. {
  18. dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  19. dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  20. dc.FillSolidRect(&lpDrawItemStruct->rcItem,
  21. ::GetSysColor(COLOR_HIGHLIGHT));
  22. }
  23. else
  24. {
  25. if(lpDrawItemStruct->itemID%2)
  26. dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128,128,128));
  27. else
  28. dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,128,255));
  29. }
  30. // If this item has the focus, draw a red frame around the
  31. // item's rect.
  32. if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
  33. (lpDrawItemStruct->itemState & ODS_FOCUS))
  34. {
  35. CBrush br(RGB(0, 0, 128));
  36. dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
  37. }
  38. // Draw the text.
  39. CString strText(_T(""));
  40. CRect rcItem;
  41. for(int i=0; i<GetHeaderCtrl()->GetItemCount(); i++)
  42. {
  43. strText = GetItemText(lpDrawItemStruct->itemID, i);
  44. GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem);
  45. rcItem.left += 5;
  46. dc.DrawText(
  47. strText,
  48. strText.GetLength(),
  49. &rcItem,
  50. DT_LEFT|DT_SINGLELINE|DT_VCENTER);
  51. }
  52. // Reset the background color and the text color back to their
  53. // original values.
  54. dc.SetTextColor(crOldTextColor);
  55. dc.SetBkColor(crOldBkColor);
  56. dc.Detach();
  57. }
  58. // 调用
  59. CNewListCtrl m_list; // 类的成员变量
  60. #define IDC_LIST 0x1101
  61. m_list.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|WS_HSCROLL|LVS_OWNERDRAWFIXED, CRect(0, 0, 280, 280), this, IDC_LIST);
  62. m_list.ModifyStyle(0, LVS_REPORT|LVS_SINGLESEL);
  63. m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
  64. m_list.InsertColumn(0, _T("AAA"), LVCFMT_LEFT, 100);
  65. m_list.InsertColumn(1, _T("BBB"), LVCFMT_LEFT, 100);
  66. CString strText(_T(""));
  67. for(int i=0; i<20; i++)
  68. {
  69. m_list.InsertItem(i, _T(""));
  70. strText.Format(_T("%d - Hello, World!"), i+1);
  71. m_list.SetItemText(i, 0, strText);
  72. strText.Format(_T("%d - ABCDEFG"), i+1);
  73. m_list.SetItemText(i, 1, strText);
  74. }

显示效果如下图所示:

自绘CListCtrl类,重载虚函数DrawItem

http://blog.csdn.net/visualeleven/article/details/5948057