发这个帖子之前,我已经发过一个,请先看看:
http://community.csdn.net/Expert/topic/3704/3704297.xml?temp=3.532046E-02我大概已经知道,可以重载OnCustomDraw函数来实现ListCtrl中颜色的重画。并且通过下面的代码来指定改变某行的颜色,lplvcd->nmcd.dwItemSpec == nLine //nLine表示listctrl第几行。小弟现在要解决的问题是:
当点击Column时,ListCtrl会对里面的数据排序。那么在排序过程中,该行的颜色和该行的数据怎么保持同步?具体的步骤是怎么样的?我找到了几个类似的例子,但是很复杂,我研究了一个礼拜天还是没有什么进展。一个字“菜”,两个字“很菜”。小弟特来请教,请高手指点,谢谢!学习,关注……

解决方案 »

  1.   

    可以先自己定义颜色类型,比如1代表红色,2代表蓝色,等等,
    ListCtrl 中有两个函数GetItemData 和SetItemData,你在第一次设置每行颜色时SetItemData,当改变行顺序时,调用GetItemData,判断颜色进行重画。
      

  2.   

    我在MsFlexGrid中遇到这个问题,排序后重新判断一次。
      

  3.   

    添加通知消息 IDC_LISTCTRL为你listctrl的ID
    BEGIN_MESSAGE_MAP(CListCtrlDlg, CDialog)
    ...
    ON_NOTIFY ( NM_CUSTOMDRAW, IDC_LISTCTRL, OnCustomdrawMyList )
    ...
    END_MESSAGE_MAP()
    请看http://www.codeproject.com/listctrl/highlightlistctrl.aspvoid CMyListCtrlView::OnCustomdrawMyList(NMHDR *pNMHDR, LRESULT *pResult)
    {
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );    // Take the default processing unless we set this to something else below.
        *pResult = CDRF_DODEFAULT; //设置listctrl的格式,由格子等

    pListCtrl.SetExtendedStyle(pListCtrl.GetExtendedStyle()  | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); //
        // 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 );
                      DWORD dwColorType = m_ctListCtrl.GetItemData(nItem);
                      switch(dwColorType)
                      {
                       case 1://方案一
                              clrNewTextColor = RGB(0,0,0); //Set the text to red
    clrNewBkColor = RGB(240,240,240); //Set the bkgrnd color to blue
                              break;
                       case 2://方案2
    clrNewTextColor = RGB(0,0,0); //Leave the text black
    clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
                              break;
                       } pLVCD->clrText = clrNewTextColor;
    pLVCD->clrTextBk = clrNewBkColor;

            
            // Tell Windows to paint the control itself.
            *pResult = CDRF_DODEFAULT;
            
            
    }
    }
    你在插入数据时要设置每行的显示方案
    int nItem = m_ctListCtrl.GetItemCount();
    m_ctListCtrl.InsertItem(nItem...);
    //设置成方案1
    m_ctListCtrl.SetItemData(nItem, (DWORD)1);
      

  4.   

    谢谢 stavck(在河之洲)太帅了,结贴!
      

  5.   

    // The pointer to my combo box.
    extern CComboBox* pmyComboBox;// Set the data of each item to be equal to its index.
    for (int i=0;i < pmyComboBox->GetCount();i++)
    {
       pmyComboBox->SetItemData(i, i);
    }