mfc 的 clistctrl 显示item的最大长度

转自:http://bbs.csdn.net/topics/80015270


需要显示快1000个字符了,但是默认的clistctrl显示不完全。

网上说重绘控件,说重载settiemtex,getitemtext的都不行。

有人说重载DrawItem的可以。

操作如下:CMyListCtrl类从CListCtrl类派生,并重载DrawItem函数,在使用的地方把CListCtrl改为CMyListCtrl,并设置CMyListCtrl成员为重画风格(或在属性中选中Owner Draw fixed复选框),肯定行。

void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rcItem(lpDrawItemStruct->rcItem);
	int nItem = lpDrawItemStruct->itemID;
	COLORREF clrTextSave, clrBkSave;
	static _TCHAR szBuff[MaxSizeOneItem];//这里是你要显示的字符串长度,想多长有多长
	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_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);
	}
	if (lvi.state & LVIS_FOCUSED)
		pDC->DrawFocusRect(rcAllLabels);
	if (bSelected)
	{
		pDC->SetTextColor(clrTextSave);
		pDC->SetBkColor(clrBkSave);
	}
}



猜你喜欢

转载自blog.csdn.net/xuepiaofei1/article/details/78805701
今日推荐