在CListCtrl中更改行的颜色 [英] Change color of row in CListCtrl

查看:460
本文介绍了在CListCtrl中更改行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,
我在代码中的复选框中使用了CListCtrl.如果要选中行的复选框,我想更改行的颜色.我尝试在"OnClickLstdata"事件上使用函数SetTextBkColor,但它会更改所有插入的行的颜色.请提供解决方案.谢谢...:)

Hi friends,
I am using CListCtrl with check boxes in my code. I want to change the color of row if its check box is checked. I have tried using function SetTextBkColor on ''OnClickLstdata'' event but it changes color of all inserted rows. Please provide the solution. Thanks... :)

推荐答案

与所有者绘制的列表控件相比,我更喜欢自定义绘制,因为您只需要绘制您感兴趣的部分,并且让控件绘制其余部分.
就您而言,您所要做的就是在正确的位置设置颜色.

I would favor custom drawing over owner drawn list controls, since you only have to draw the part you''re interested in, and let the control draw the rest.
In your case, all you have to do is set a color at the right place.

void CMyListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);

	switch (pNMCD->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
		*pResult = CDRF_NOTIFYITEMDRAW;
		break;
	case CDDS_ITEMPREPAINT:
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		break;
	case (CDDS_ITEMPREPAINT | CDDS_SUBITEM):
		if (HasDifferentColor(pNMCD->iItem))
			pNMCD->clrTextBk = m_clrDifferent;
		break;
...


更正了最后一个大小写常量


Corrected the last case constant


您必须从CListCtrl派生一个新类,并覆盖OnPaint()事件,以所需的方式绘制该项目.请记住,执行此操作时必须处理所有绘画情况.
You have to derive a new class from CListCtrl, and override the OnPaint() event, drawing the item the way you want to. Keep in mind that you have to handle ALL painting situations when you do this.


我并没有这样做,但是没有人回答...

如果要执行此操作,请从列表视图控件中原始" SDK样式句柄WM_NOTIFY并处理NM_CUSTOMDRAW通知.这将告诉您正在绘制什么,并使您能够控制过程.

正如您似乎在使用MFC一样,如何重写DrawItem()?那应该告诉您所有您需要了解的内容,并使您能够自定义流程.

无论如何,请查看 [
I''ve not done this in yonks but as no one else has answered...

If you want to do it "raw" SDK style handle WM_NOTIFY from the list view control and process the NM_CUSTOMDRAW notification. That will tell you what''s being drawn and enable you to control the process.

As you seem to using MFC how about overriding DrawItem()? That should tell you everything you need to know and enable you to customise the process.

Anyway, have a look at this[^]article on MSDN for some background, especially the sections on custom drawing.

Hope that points you in the right direction,

Cheers,

Ash


这篇关于在CListCtrl中更改行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆