欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-28 19:40:58
...

效果展示

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

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;
}