class newctrl : public CListCtrl
{
// Construction
public:
newctrl();
// Attributes
public:
UINT m_nItemHeight;
// Operations
public:
void SetItemHeight(UINT nHeight);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(newctrl)
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~newctrl();
// Generated message map functions
protected:
//{{AFX_MSG(newctrl)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
newctrl::newctrl()
{
m_nItemHeight=0;
}
newctrl::~newctrl()
{
}
BEGIN_MESSAGE_MAP(newctrl, CListCtrl)
//{{AFX_MSG_MAP(newctrl)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_MEASUREITEM_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// newctrl message handlers
void newctrl::SetItemHeight(UINT nHeight)
{
m_nItemHeight = nHeight;
CRect rcWin;
GetWindowRect(&rcWin);
WINDOWPOS wp;
wp.hwnd = m_hWnd;
wp.cx = rcWin.Width();
wp.cy = rcWin.Height();
wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}
void newctrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
TCHAR lpBuffer[256];
LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_PARAM ;
lvi.iItem = lpDrawItemStruct->itemID ;
lvi.iSubItem = 0;
lvi.pszText = lpBuffer ;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;
for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
{
if ( nCol > 0 )
{
// Get Previous Column Width in order to move the next display item
GetColumn(nCol-1, &lvcprev) ;
lpDrawItemStruct->rcItem.left += lvcprev.cx ;
lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ;
}
// Get the text
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT)) ;
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
}
else
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ;
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;
}
pDC->SelectObject(GetStockObject(SYSTEM_FONT));
UINT uFormat=DT_LEFT ;
::DrawText(lpDrawItemStruct->hDC, lpBuffer, wcslen(lpBuffer),
&lpDrawItemStruct->rcItem, uFormat) ;
pDC->SelectStockObject(SYSTEM_FONT) ;
}
}
void newctrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if(m_nItemHeight>0)
lpMeasureItemStruct->itemHeight=m_nItemHeight;
}
注意事项:
这几个函数都必须手动添加,不能够用继承的函数