1.设置ListCtrl属性为Owner Draw Fixed;
2.重写DrawItem
【转自: http://blog.csdn.net/zwdheo/article/details/2065576】
2.重写DrawItem
void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect(lpDrawItemStruct->rcItem); //取得绘制区域
int iItem = lpDrawItemStruct->itemID; //取得行
TCHAR szBuff[MAX_PATH];
LVITEM lvItem; //取得Item所需LVITEM结构体
int iColumn = 0; //DRAWITEMSTRUCT中不包含有item的列信息
CHeaderCtrl *Head = GetHeaderCtrl();
int iMax = Head->GetItemCount(); //取得列总数
while(iMax > iColumn)
{
//以列为单位进行处理,可根据自己的要求进行处理
//取得Item所需设置
lvItem.mask = LVIF_TEXT;// | LVIF_IMAGE | LVIF_STATE | LVIF_INDENT;
lvItem.iItem = iItem;
lvItem.iSubItem = iColumn;
lvItem.pszText = szBuff;
lvItem.cchTextMax = sizeof(szBuff);
// lvItem.stateMask = LVIS_ACTIVATING | LVIS_FOCUSED | LVIS_SELECTED | LVIS_STATEIMAGEMASK;
COLORREF txtColor; //文本颜色
DWORD uformat; //文本格式
CFont font; //文本字体
LOGFONT lf;
if (iColumn < 1)
{
//设置字体
GetFont()->GetLogFont( &lf );
lf.lfHeight = abs(lf.lfHeight) + 2;
lf.lfWeight = FW_BOLD;
VERIFY(font.CreateFontIndirect(&lf));
pDC->SelectObject(font.m_hObject);
//设置颜色
txtColor = GetSysColor(COLOR_WINDOWTEXT);
pDC->SetTextColor(txtColor);
//取得绘制区域
GetSubItemRect(iItem, iColumn, LVIR_BOUNDS, rect);
//设置文本格式
uformat = DT_WORD_ELLIPSIS | DT_SINGLELINE | DT_LEFT | DT_VCENTER;
}
else
{
GetFont()->GetLogFont( &lf );
txtColor = GetSysColor(COLOR_GRAYTEXT);
pDC->SelectObject(GetFont()->m_hObject);
pDC->SetTextColor(txtColor );
GetSubItemRect(iItem, iColumn, LVIR_BOUNDS, rect);
uformat = DT_WORD_ELLIPSIS | DT_SINGLELINE | DT_RIGHT | DT_VCENTER;
}
GetItem(&lvItem);
pDC->DrawText(lvItem.pszText, rect, uformat); //绘制文本
iColumn++;
}
}
【转自: http://blog.csdn.net/zwdheo/article/details/2065576】