单元格长度256限制问题 ....替换
重载DrawItem时记得OwnerDrawFixed 勾选上,才能生效
MyListCtrl.h
class CMyListCtrl : public CListCtrl
{
public:
CMyListCtrl();
void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual ~CMyListCtrl();
};
MyListCtrl.cpp
// MyListCtrl.cpp: implementation of the CMyListCtrl class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyListCtrl.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyListCtrl::CMyListCtrl()
{
}
CMyListCtrl::~CMyListCtrl()
{
}
void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{/*
CRect rcRow,rcText;
CString strText;
int nColCount;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
int nSavedDC = pDC->SaveDC();
rcRow = lpDrawItemStruct ->rcItem;
int nID=lpDrawItemStruct->itemID;
nColCount = GetHeaderCtrl()->GetItemCount();
if (lpDrawItemStruct->itemState >1)
{
pDC->FillRect(rcRow,&CBrush(GetSysColor(COLOR_HIGHLIGHT)));
}
else
pDC->FillRect(rcRow,&CBrush(GetSysColor(COLOR_HIGHLIGHTTEXT)));
for (int i=0; i<nColCount; i++)
{
CRect rc;
strText = GetItemText(nID,i);
rcText = rcRow;
GetHeaderCtrl()->GetItemRect(i,rc);
rcText.left = rc.left+4;
rcText.right = rc.right;
GetSubItemRect(0,0,LVIR_BOUNDS,rc);
pDC->SetBkMode(TRANSPARENT);
if(lpDrawItemStruct->itemState>1)
pDC->SetTextColor(RGB(255,255,255));
else
pDC->SetTextColor(RGB(0,0,0));
rcText.left += rc.left;
rcText.right += rc.left;
pDC->DrawText(strText,rcText,DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
}
pDC->RestoreDC(nSavedDC);
*/
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
int nSavedDC = pDC->SaveDC();
CRect rcItem(lpDrawItemStruct->rcItem);
int nItem = lpDrawItemStruct->itemID;
COLORREF clrTextSave, clrBkSave;
static _TCHAR szBuff[512]; //这里是你要显示的字符串长度,想多长有多长
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 = 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_NOPREFIX|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
}
if (lvi.state & LVIS_FOCUSED)
pDC->DrawFocusRect(rcAllLabels);
if (bSelected)
{
pDC->SetTextColor(clrTextSave);
pDC->SetBkColor(clrBkSave);
}
pDC->RestoreDC(nSavedDC);
}