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;
   }  
 }
}

 

///

要改变listctrl单元格的背景及文字颜色,可以按照以下步骤进行: 1. 首先,我们需要为listctrl添加一个自定义的绘制器(custom class)。这可以通过继承listctrl的wx.ListCtrl类,并重写其中的OnItemAttr方法来实现。 2. 在自定义的绘制器类中,我们可以通过设置wx.ListItem的不同属性来改变特定单元格的背景和文字颜色。例如,可以使用SetBackgroundColour和SetTextColour方法来设置特定单元格的背景和文字颜色。 3. 然后,我们需要将listctrl与自定义的绘制器关联起来。可以使用SetCustomAttrProvider方法将自定义的绘制器类实例与listctrl相关联。 4. 最后,在listctrl中添加项目时,我们可以为每个单元格设置不同的背景和文字颜色。可以通过使用wx.ListCtrl的SetItem方法设置特定单元格的wx.ListItem的属性。 下面是一个示例代码,展示了如何改变listctrl单元格的背景和文字颜色: ```python import wx class CustomListCtrl(wx.ListCtrl): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.attrProvider = CustomAttrProvider() self.SetCustomAttrProvider(self.attrProvider) class CustomAttrProvider(wx.ListItemAttrProvider): def __init__(self): super().__init__() self.attr = wx.ListItemAttr() def OnItemAttr(self, item): return self.attr def SetAttr(self, item, attr): self.attr = attr class MyFrame(wx.Frame): def __init__(self): super().__init__(None, title='ListCtrl示例', size=(400, 300)) panel = wx.Panel(self) sizer = wx.BoxSizer(wx.VERTICAL) listctrl = CustomListCtrl(panel, style=wx.LC_REPORT) listctrl.InsertColumn(0, '姓名') listctrl.InsertColumn(1, '年龄') # 设置第一行单元格的背景和文字颜色 item1 = wx.ListItem() item1.SetText('张三') item1.SetColumn(0) itemAttr1 = wx.ListItemAttr() itemAttr1.SetBackgroundColour(wx.RED) itemAttr1.SetTextColour(wx.WHITE) listctrl.SetItem(item1) listctrl.SetItemAttr(itemAttr1) # 设置第二行单元格的背景和文字颜色 item2 = wx.ListItem() item2.SetText('李四') item2.SetColumn(0) itemAttr2 = wx.ListItemAttr() itemAttr2.SetBackgroundColour(wx.GREEN) itemAttr2.SetTextColour(wx.BLACK) listctrl.SetItem(item2) listctrl.SetItemAttr(itemAttr2) sizer.Add(listctrl, 1, wx.EXPAND) panel.SetSizer(sizer) if __name__ == '__main__': app = wx.App() frame = MyFrame() frame.Show() app.MainLoop() ``` 在这个示例中,我们创建了一个自定义的ListCtrl类CustomListCtrl,以及一个自定义的绘制器类CustomAttrProvider。然后,我们在自定义的ListCtrl中关联了CustomAttrProvider。在MyFrame的init方法中,我们创建了一个CustomListCtrl,并为其中的单元格设置不同的背景和文字颜色。运行代码后,你将看到listctrl中的单元格的背景和文字颜色已经改变了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值