1,设置重绘
2,新建类,继承CListCtrl
#pragma once #include "afxcmn.h" class DBufferListCtrl : public CListCtrl { public: DBufferListCtrl(void); ~DBufferListCtrl(void); DECLARE_MESSAGE_MAP() afx_msg void OnPaint(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); CString GetItemText(int nItem, int nSubItem) const; virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/); }; #include "stdafx.h" #include "DBufferListCtrl.h" DBufferListCtrl::DBufferListCtrl(void) { } DBufferListCtrl::~DBufferListCtrl(void) { } BEGIN_MESSAGE_MAP(DBufferListCtrl, CListCtrl) ON_WM_PAINT() ON_WM_ERASEBKGND() END_MESSAGE_MAP() void DBufferListCtrl::OnPaint() { CPaintDC dc(this); // device context for painting CRect rect; CRect headerRect; CDC MenDC; CBitmap MemMap; GetClientRect(&rect); GetDlgItem(0)->GetWindowRect(&headerRect); MenDC.CreateCompatibleDC(&dc); MemMap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); MenDC.SelectObject(&MemMap); MenDC.FillSolidRect(&rect,RGB(255,255,255)); DefWindowProc(WM_PAINT,(WPARAM)MenDC.m_hDC,(LPARAM)0); dc.BitBlt(0, headerRect.Height(), rect.Width(), rect.Height(), &MenDC, 0, headerRect.Height(), SRCCOPY); MenDC.DeleteDC(); MemMap.DeleteObject(); } BOOL DBufferListCtrl::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default //return CListCtrl::OnEraseBkgnd(pDC); return FALSE; } CString DBufferListCtrl::GetItemText(int nItem, int nSubItem) const { ASSERT(::IsWindow(m_hWnd)); LVITEM lvi; memset(&lvi, 0, sizeof(LVITEM)); lvi.iSubItem = nSubItem; CString str; int nLen = 512; int nRes; do { nLen *= 2; lvi.cchTextMax = nLen; lvi.pszText = str.GetBufferSetLength(nLen); nRes = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT, (WPARAM)nItem, (LPARAM)&lvi); } while (nRes >= nLen-1); str.ReleaseBuffer(); return str; } void DBufferListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rcItem(lpDrawItemStruct->rcItem); int nItem = lpDrawItemStruct->itemID; COLORREF clrTextSave, clrBkSave; static _TCHAR szBuff[1024]; LV_ITEM lvi; lvi.mask = LVIF_TEXT | LVIF_STATE;//LVIF_IMAGE | lvi.iItem = nItem; lvi.iSubItem = 0; lvi.pszText = szBuff; lvi.cchTextMax = sizeof(szBuff); lvi.stateMask = 0xFFFF; GetItem(&lvi); BOOL bSelected = (lvi.state & LVIS_SELECTED); CRect rcAllLabels; GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS); if (bSelected) { clrTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT)); clrBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); pDC->FillRect(rcAllLabels, &CBrush(::GetSysColor(COLOR_HIGHLIGHT))); } GetItemRect(nItem, rcItem, LVIR_LABEL); pDC->DrawText(szBuff, -1, rcItem, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER); LV_COLUMN lvc; lvc.mask = LVCF_FMT | LVCF_WIDTH; for (int nColumn = 1; GetColumn(nColumn, &lvc); nColumn++) { rcItem.left = rcItem.right; rcItem.right += lvc.cx; int nRetLen = CListCtrl::GetItemText(nItem, nColumn, szBuff, sizeof(szBuff)); if (nRetLen == 0) continue; UINT nJustify = DT_LEFT; switch (lvc.fmt & LVCFMT_JUSTIFYMASK) { case LVCFMT_RIGHT: nJustify = DT_RIGHT; break; case LVCFMT_CENTER: nJustify = DT_CENTER; break; default: break; } pDC->DrawText(szBuff, -1, rcItem, nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER); } if (lvi.state & LVIS_FOCUSED) pDC->DrawFocusRect(rcAllLabels); if (bSelected) { pDC->SetTextColor(clrTextSave); pDC->SetBkColor(clrBkSave); } }