改变过CListCtrl颜色的高手请进:ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, OnCustomdrawList)
以下代码确实可以改变ListCtrl中某Item的颜色:
void CCStyleSampleDlg::OnCustomdrawList ( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
// Take the default processing unless we set this to something else below.
*pResult = 0;
// 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 prepaint stage for an item. Here's where we set the
// item's text color. Our return value will tell Windows to draw the
// item itself, but it will use the new color we set here.
// We'll cycle the colors through red, green, and light blue.
COLORREF crText;
if ( (pLVCD->nmcd.dwItemSpec % 3) == 0 )
crText = RGB(255,0,0);
else if ( (pLVCD->nmcd.dwItemSpec % 3) == 1 )
crText = RGB(0,255,0);
else
crText = RGB(128,128,255);
// Store the color back in the NMLVCUSTOMDRAW struct.
pLVCD->clrText = crText;
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}
但是代码中变换颜色没有根据插入ListCtrl项的内容来变化,没有任何意义。
我的意思是:crText颜色的变化要根据我要插入的内容来变化,这才有实际意义。
插入的内容来自一个lpszRow,但这个lpszRow无法关联到crText,具体请看下面线程:
UINT Thread_Insert_Item(LPVOID )
{
int index,rand_Num,row,count,k,i;
CString temp;
while(!StopThread)
{
Sleep(3000);
dlg->m_list.DeleteAllItems();
for(i=0;i<15;i++)
{
srand( (unsigned)time( NULL ));
rand_Num=rand();
rand_Num+=i;
row=rand_Num%6;
temp=lpszRow[row*4]; //lpszRow是一个缓冲区
k=rand_Num%4;
////这里要根据lpszRow的内容变换颜色,但在此设置crText,没有效果
index=dlg->m_list.InsertItem(0,temp,k);
dlg->m_list.SetItemText(index,1,lpszRow[row*4+1]);
dlg->m_list.SetItemText(index,2,lpszRow[row*4+2]);
dlg->m_list.SetItemText(index,3,lpszRow[row*4+3]);
}
}
return 0;
}
我的困惑是颜色变化没有根据插入的内容来变。
问题点数:50、回复次数:4Top
1 楼psbeond(LibUIDK界面库客服)回复于 2005-06-03 12:30:08 得分 0
插入完成后用dlg->m_list.InvalidateRect(NULL)刷新一下Top
2 楼jiangsheng(蒋晟.Net[MVP])回复于 2005-06-03 13:06:12 得分 35
你可以在插入时用LVITEM结构的lparam成员保存数据,在处理CDDS_ITEMPREPAINT的时候从pLVCD中获取数据。Top
3 楼koko1998(高价购买火车票)回复于 2005-06-03 13:10:26 得分 15
http://www.vckbase.com/code/downcode.asp?id=2146
http://www.vckbase.com/code/downcode.asp?id=1920
http://www.vckbase.com/document/viewdoc/?id=891
http://www.codeproject.com/listctrl/xlistctrl.asp
http://www.codeproject.com/listctrl/ReportControl.aspTop
4 楼xjtt2000(沧海一笑)回复于 2005-06-03 15:01:17 得分 0
jiangsheng:
你的方法也许最快,但如何做呢?
pLVCD中怎么获取数据?Top




