设置ListCtrl列表控件其中某一行的字体和背景颜色

设置ListCtrl列表控件其中某一行的字体和背景颜色,可以最终达到如下效果:


操作步骤如下所示:

1.先添加一个自定义消息

ON_NOTIFY ( NM_CUSTOMDRAW,IDC_V_H264_NALLIST, OnCustomdrawMyList )

注:在BEGIN_MESSAGE_MAP()和END_MESSAGE_MAP()之间。第二个参数是LIstCtrl的ID,第三个参数是消息响应函数。

2.下面是具体的函数。根据表格内容的不同,设置不同的背景颜色。

比如:“NAL负载类型”为SLICE的时候,背景为青色;为SPS的时候,背景为黄色;为PPS的时候,背景为咖啡色。

//ListCtrl加颜色
void CSpecialVH264Dlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
	//This code based on Michael Dunn's excellent article on
	//list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp


	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );


	// Take the default processing unless we set this to something else below.
	*pResult = CDRF_DODEFAULT;


	// First thing - check the draw stage. If it's the control's prepaint
	// stage, then tell Windows we want messages for every item.
	if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
	{
		// This is the notification message for an item.  We'll request
		// notifications before each subitem's prepaint stage.


		*pResult = CDRF_NOTIFYSUBITEMDRAW;
	}
	else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
	{


		COLORREF clrNewTextColor, clrNewBkColor;


		int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );


		CString strTemp = m_vh264nallist.GetItemText(nItem,2);
		if(strcmp(strTemp,"SLICE")==0){
			clrNewTextColor = RGB(0,0,0);		//Set the text 
			clrNewBkColor = RGB(0,255,255);		//青色
		}
		else if(strcmp(strTemp,"SPS")==0){
			clrNewTextColor = RGB(0,0,0);		//text 
			clrNewBkColor = RGB(255,255,0);		//黄色
		}
		else if(strcmp(strTemp,"PPS")==0){
			clrNewTextColor = RGB(0,0,0);		//text
			clrNewBkColor = RGB(255,153,0);		//咖啡色
		}else if(strcmp(strTemp,"SEI")==0){
			clrNewTextColor = RGB(0,0,0);		//text
			clrNewBkColor = RGB(255,66,255);			//粉红色
		}else if(strcmp(strTemp,"IDR_SLICE")==0){
			clrNewTextColor = RGB(0,0,0);		//text
			clrNewBkColor = RGB(255,0,0);			//红色
		}else{
			clrNewTextColor = RGB(0,0,0);		//text
			clrNewBkColor = RGB(255,255,255);			//白色
		}


		pLVCD->clrText = clrNewTextColor;
		pLVCD->clrTextBk = clrNewBkColor;




		// Tell Windows to paint the control itself.
		*pResult = CDRF_DODEFAULT;




	}
}


  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
MFC中的LISTCTRL控件默认不支持自动换行,但可以通过以下步骤实现: 1. 在LISTCTRL控件的父窗口中添加一个CStatic控件,作为LISTCTRL控件的背景; 2. 在LISTCTRL控件的父窗口中重载WM_PAINT消息处理函数,在其中遍历LISTCTRL中所有单元格,将单元格中的文本进行绘制,并根据需要进行自动换行; 3. 在LISTCTRL控件的各个单元格的编辑和修改事件中,更新CStatic控件中的相应文本。 具体实现代码可以参考以下示例: ``` // 声明CMyListCtrl类,继承自CListCtrl class CMyListCtrl : public CListCtrl { public: afx_msg void OnPaint(); DECLARE_MESSAGE_MAP() }; // 在父窗口类中添加CStatic控件和CMyListCtrl控件,并在WM_PAINT消息中调用CMyListCtrl的OnPaint函数 void CMyDialog::OnPaint() { CPaintDC dc(this); CRect rect; m_list.GetClientRect(rect); // 创建背景静态控件 if (!m_static.GetSafeHwnd()) { m_static.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_LEFT, rect, this, 0); } // 遍历所有行和列,将文本绘制到静态控件中 CString strText; CRect cellRect, textRect; int nRow = GetItemCount(); int nCol = GetHeaderCtrl()->GetItemCount(); for (int i = 0; i < nRow; i++) { for (int j = 0; j < nCol; j++) { strText = GetItemText(i, j); // 获取单元格的矩形区域 GetSubItemRect(i, j, LVIR_LABEL, cellRect); textRect = cellRect; // 根据文本长度和单元格宽度计算需要的行数 int nLines = 1; CSize sizeText = dc.GetTextExtent(strText); if (sizeText.cx > cellRect.Width()) { nLines = (sizeText.cx + cellRect.Width() - 1) / cellRect.Width(); textRect.bottom += (nLines - 1) * dc.GetTextExtent(_T("A")).cy; } // 绘制文本 dc.DrawText(strText, textRect, DT_LEFT | DT_TOP | DT_WORDBREAK); } } } // 在单元格的编辑和修改事件中,更新静态控件中的文本 void CMyDialog::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult) { LV_DISPINFO* pDispInfo = reinterpret_cast<LV_DISPINFO*>(pNMHDR); LV_ITEM* pItem = &(pDispInfo->item); CString strText = pItem->pszText; m_static.SetWindowText(strText); *pResult = 0; } ```
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值