在单个clistctrl行中获取背景色 [英] Getting background color in a single clistctrl row

查看:77
本文介绍了在单个clistctrl行中获取背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何使用自定义绘图设置颜色的文章很多,但是每当我尝试获取当前的背景颜色时,我总是会得到0xffff.我在哪里出错?

There are lots of articles on how to set color using custom draw but whenever I try to get the current background color I always get 0xffff back. Where am I going wrong?

推荐答案

GetBkColor()SetBkColor()将获得并设置所有列表项的默认颜色.如果使用自定义绘制更改单个行或单元格的颜色,则颜色值将作为参数传递给绘制函数,而不存储在任何地方.如果以后要检索特定行的颜色,则必须将值存储在某个位置或实现一个函数,该函数可以使用与自定义绘制处理程序中相同的条件来检测已传递的值.

一种选择是使用列表控件项的数据成员.您可以使用SetItemData()将标记值存储在该成员中,然后在自定义绘图功能中根据该值选择颜色.要确定已设置的颜色,只需使用GetItemData()检查标志值.

为单行设置标志值时,可以使用RedrawItems()仅重绘受影响的行.

例如:

GetBkColor() and SetBkColor() will get and set the default color for all list items. If you use custom draw to change the color of individual rows or cells, the color value is passed as argument to the drawing function and not stored anywhere. If you want to retrieve the color for specific rows later, you must store the value somewhere or implement a function that can detect the value that has been passed using the same condition as in the custom draw handler.

One option would be to use the data member of list control items. You may store a flag value in that member using SetItemData() and select the colors according to that value in the custom draw function. To determine which color has been set, just check the flag value using GetItemData().

When setting the flag value for a single row, you can use RedrawItems() to redraw only the affected row.

For example:

void CDDListCtrl::SetFlag(int nItem, DWORD dwFlag)
{
    SetItemData(nItem, dwFlag);
    RedrawItems(nItem, nItem);
}
void CDDListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
...
    // I''m not sure if cd->nmcd.lItemlParam can be used here
    DWORD dwFlag = GetItemData(cd->nmcd.dwItemSpec);
    // set color here according to flag value and sub-item
}
COLORREF CDDListCtrl::GetCellColor(int nItem, int nSubItem)
{
    DWORD dwFlag = GetItemData(nItem);
    // return color here according to flag value and sub-item
}


谢谢.使用Custon Draw我可以设置单个行的背景颜色没有问题.更改它.我尝试了getbkcolor和gettextbkcolor的所有组合,只是把垃圾弄回来了.

Hi Thank you.. Using Custon Draw I can set the background color of individual rows no problem.. But then I want to find out what the color is in a particular row to change it..I have tried all combination of getbkcolor and gettextbkcolor and just get rubbish back..

void CDDListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* cd = reinterpret_cast<nmlvcustomdraw*>(pNMHDR); 

*pResult = CDRF_DODEFAULT;

if ( CDDS_PREPAINT == cd->nmcd.dwDrawStage )

  *pResult = CDRF_NOTIFYITEMDRAW;
  }
else if ( CDDS_ITEMPREPAINT == cd->nmcd.dwDrawStage )
	{
  *pResult = CDRF_NOTIFYSUBITEMDRAW;
  }
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == cd->nmcd.dwDrawStage )
  {
  	if (cd->nmcd.hdr.idFrom == IDC_PLAYLIST)
		{
		if (PlaylistInfo.m_AddDataType == ADD_TYPE_NORMAL)
			{
			if ((RegValue == SINGLE_MANUAL) || (RegValue == SINGLE_RANDOM) || (RegValue == SINGLE_FORMAT))
				{
				cd->clrTextBk = RED;
				}
			else
				{
				cd->clrTextBk = GREEN;
				}
			}
		}
	if (PlaylistInfo.m_AddDataType == ADD_TYPE_SEGUE)
		{
		if ( 1 == cd->iSubItem )
			{
		cd->clrTextBk = GetTextBkColor();
		SetTextBkColor(cd->clrTextBk);


这篇关于在单个clistctrl行中获取背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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