mfc之ClistCtrl控件自绘,实现修改列表框的行高和字的大小,间隔的修改某行颜色和高亮颜色

15 篇文章 1 订阅
3 篇文章 1 订阅

效果展示


1.添加消息响应函数

.h中添加(class CBaseEditListCtrl : public CListCtrl)

int m_nRowHeight;

void SetRowHeigt(int nHeight);

afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);

afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);

virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

ON_WM_MEASUREITEM_REFLECT()

ON_WM_MEASUREITEM()

ON_WM_DRAWITEM()

void CBaseEditListCtrl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
 {
	 CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);  
 }

 void CBaseEditListCtrl::SetRowHeigt(int nHeight)
 {
	 m_nRowHeight = 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 CBaseEditListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
 {

	 CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);    
	 LVITEM lvi = {0}; 
	 lvi.mask = LVIF_STATE;//|LVIF_IMAGE; 
	 lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ; 
	 lvi.iItem = lpDrawItemStruct->itemID; 
	 BOOL bGet = GetItem(&lvi); 
	
	 //高亮显示  
	 BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED) 
		 && ((GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS))));

	 //画文本背景 
	 CRect rcBack = lpDrawItemStruct->rcItem; 
	// pDC->SetBkMode(TRANSPARENT); 

	 if( bHighlight ) //如果被选中
	 { 
		 pDC->SetTextColor(RGB(255,255,255));              //文本颜色
		 pDC->FillRect(rcBack, &CBrush(RGB(90,162,100)));  //行背景色
	 } 
	 else 
	 { 
		int iRow = lvi.iItem;
		if(iRow % 2 == 0)
		{
			pDC->SetTextColor(RGB(0,0,0));                    //文本颜色
			pDC->FillRect(rcBack, &CBrush(RGB(211,223,238))); //行背景色
		}
	 } 

	 //绘制文本
	 if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE) 
	 { 
		 //得到列数
		 int nCollumn = GetHeaderCtrl()->GetItemCount();

		 //循环处理
		 CString szText; 
		 for (int i = 0; i < GetHeaderCtrl()->GetItemCount(); i++) 
		 {  
			 CRect rcItem; 
			 if ( !GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem ))
			 { 
				 continue; 
			 }

			 szText = GetItemText( lpDrawItemStruct->itemID, i );

			 rcItem.left += 5; rcItem.right -= 1; 
			 pDC->DrawText(szText, lstrlen(szText), &rcItem,  
				 DT_LEFT | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE);  
		 } 
	 } 
 }

4.将ListCtrl控件属性"Owner DrawFixed"设置为TRUE,"Owner Data"设置为FALSE(默认为FLASE);

if(!m_myListCtrl.Create(WS_VISIBLE|WS_GROUP|WS_CHILD|WS_VSCROLL|WS_HSCROLL| WS_TABSTOP|LVS_REPORT|LVS_OWNERDRAWFIXED,CRect(0,0,300,400),this,1))
return FALSE;

m_myListCtrl.SetRowHeigt(30);


方法2:只改变间隔的修改某行的颜色

void CBaseEditListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW lpLVCustomDraw = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	switch ( lpLVCustomDraw->nmcd.dwDrawStage )
	{
	case CDDS_PREPAINT:
		{
			*pResult = CDRF_NOTIFYITEMDRAW;
		}
		break;
	case CDDS_ITEMPREPAINT :
		{
			if ( lpLVCustomDraw->nmcd.dwItemSpec % 2 == 0)
			{
				lpLVCustomDraw->clrText = RGB(0,0 ,0);  // 设置字体颜色
				lpLVCustomDraw->clrTextBk = RGB(211,223,238); // 背景色
			}
			else
			{
				lpLVCustomDraw->clrText = RGB(0,0, 0);
				lpLVCustomDraw->clrTextBk = RGB(255,255,255);
			}

			*pResult = CDRF_NOTIFYITEMDRAW/*CDRF_DODEFAULT*/; 
		}
		break;

	}
	*pResult = 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值