ListCtrl设置单个单元格颜色的实现 .

实现这个的最简单的方法就是重载ListCtrl的NM_CUSTOMDRAW消息,如下:

ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &Clistctrl_testDlg::OnNMCustomdrawList1)

然后在相关函数中添加处理代码:

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

 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 )
 {
  int nItem=static_cast<int>(pLVCD->nmcd.dwItemSpec );
  switch(pLVCD->iSubItem)
  {
  case 2:    break;
  case 4:    //选择第四列
   {
    if (nItem % 2 == 0)//选择偶数行
    {

     COLORREF clrNewTextColor, clrNewBkColor;
     clrNewTextColor = RGB(0,0,0);
     clrNewBkColor = RGB(198,202,198);

     pLVCD->clrText =clrNewTextColor;
     pLVCD->clrTextBk =clrNewBkColor;
     *pResult = CDRF_DODEFAULT;
     break;
    }
   }   
  default:
   pLVCD->clrText = RGB(0,0,0);
   pLVCD->clrTextBk = RGB(255,255,255);
   *pResult = CDRF_DODEFAULT;
   break;
  }
 }

 

///改进一下/

m_ListCtrl.DeleteAllItems();
 m_ListCtrl.InsertItem(0,"張三");
 m_ListCtrl.SetItemText(0,1,"178CM");
 m_ListCtrl.SetItemText(0,2,"70KG");
 m_ListCtrl.SetItemText(0,3,"14");
 m_ListCtrl.SetItemText(0,4,"男");
 m_ListCtrl.SetItemText(0,5,"籃球");


 m_ListCtrl.InsertItem(1,"王五");
 m_ListCtrl.SetItemText(1,1,"158cm");
 m_ListCtrl.SetItemText(1,2,"70kg");
 m_ListCtrl.SetItemText(1,3,"12");
 m_ListCtrl.SetItemText(1,4,"男");
 m_ListCtrl.SetItemText(1,5,"乒乓球");

 m_ListCtrl.InsertItem(2,"阿花");
 m_ListCtrl.SetItemText(2,1,"168cm");
 m_ListCtrl.SetItemText(2,2,"60kg");
 m_ListCtrl.SetItemText(2,3,"13");
 m_ListCtrl.SetItemText(2,4,"女");
 m_ListCtrl.SetItemText(2,5,"排球");
 
 m_ListCtrl.InsertItem(3,"小華");
 m_ListCtrl.SetItemText(3,1,"168cm");
 m_ListCtrl.SetItemText(3,2,"60kg");
 m_ListCtrl.SetItemText(3,3,"13");
 m_ListCtrl.SetItemText(3,4,"男");
 m_ListCtrl.SetItemText(3,5,"爬山");

 m_ListCtrl.InsertItem(4,"大聲");
 m_ListCtrl.SetItemText(4,1,"168cm");
 m_ListCtrl.SetItemText(4,2,"60kg");
 m_ListCtrl.SetItemText(4,3,"16");
 m_ListCtrl.SetItemText(4,4,"女");
 m_ListCtrl.SetItemText(4,5,"游泳");

 m_ListCtrl.InsertItem(5,"高波");
 m_ListCtrl.SetItemText(5,1,"168cm");
 m_ListCtrl.SetItemText(5,2,"60kg");
 m_ListCtrl.SetItemText(5,3,"17");
 m_ListCtrl.SetItemText(5,4,"男");
 m_ListCtrl.SetItemText(5,5,"跑步");

 

void CaacolorDlg::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

 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 )
 {
  int nItem=static_cast<int>(pLVCD->nmcd.dwItemSpec );

   switch(pLVCD->iSubItem)
   {
    case 2:break;   
   case 4:   //列 nItem % 2 == 0  m_ListCtrl.GetItemText(1,3)!="13"
    {     
     //static int i=0; 
     for(int i=0;i<6;i++)
     {      
      if ((nItem==i)&&(m_ListCtrl.GetItemText(i,3)=="13"))//行
      {       
       COLORREF clrNewTextColor, clrNewBkColor;
       clrNewTextColor = RGB(0,0,0);
       clrNewBkColor = RGB(255,255,0);

       pLVCD->clrText =clrNewTextColor;
       pLVCD->clrTextBk =clrNewBkColor;
       *pResult = CDRF_DODEFAULT;
       break;
              
      }

     }
     
     break;
    } 
    case 5:
     {
      pLVCD->clrText = RGB(0,0,0);
      pLVCD->clrTextBk = RGB(255,255,255);
     }
     
     break;
   default:
    //pLVCD->clrText = RGB(0,0,0);
    //pLVCD->clrTextBk = RGB(255,255,255);
    *pResult = CDRF_DODEFAULT;
    break;
   }  
 }
}

 

///

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值