CListCtrl, 重载DrawItem函数

定义你自己的类,继承CListCtrl类,然后重载DrawItem函数,并保证在属性中有LVS_OWNERDRAWFIXED风格.

exp:
CListCtrlExt m_ListCtrl;
m_ListCtrl.ModifyStyle(0,LVS_OWNERDRAWFIXED,0);

void CListCtrlExt::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
int nItem=lpDIS->itemID;
if(nItem == -1)
return ;
CRect rcCol = lpDIS->rcItem;
CString sText;

CDC* pDC=CDC::FromHandle(lpDIS->hDC);
int nOldDCMode=pDC->SaveDC();

LVITEM item;
item.iItem = nItem;
item.iSubItem = 0;
item.mask = LVIF_IMAGE|LVIF_STATE;
item.stateMask = 0XFFFF;
GetItem(&item);
BOOL bSelected = item.state&LVIS_SELECTED;

COLORREF color=::GetSysColor(COLOR_WINDOW);
if(bSelected )
{
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
color=::GetSysColor(COLOR_HIGHLIGHT);
}
else
{
pDC->SetBkColor(color);
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
}

LV_COLUMN lvc;
lvc.mask=LVCF_FMT|LVCF_WIDTH;

rcCol.right = rcCol.left;
for(int nCol=0; GetColumn(nCol,&lvc); nCol++)
{
rcCol.left = rcCol.right;
rcCol.right = rcCol.left + GetColumnWidth(nCol);
HPEN hOldPen = (HPEN)::SelectObject(lpDIS->hDC, ::CreatePen(PS_SOLID, 1, RGB(0xc0,0xc0,0xc0)));
HBRUSH hOldBrush = (HBRUSH)::SelectObject(lpDIS->hDC, ::CreateSolidBrush(color));
::Rectangle(lpDIS->hDC, rcCol.left-1, rcCol.top-1, rcCol.right, rcCol.bottom);
::DeleteObject(SelectObject(lpDIS->hDC, hOldBrush));
::DeleteObject(SelectObject(lpDIS->hDC, hOldPen));

sText=MakeShortString(pDC,GetItemText(nItem,nCol),rcCol.Width());
pDC->DrawText(sText, -1, CRect::CRect(rcCol.left+3,rcCol.top,rcCol.right,rcCol.bottom-2), DT_LEFT);
}
pDC->RestoreDC(nOldDCMode);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值