CListCtrl使用小技巧,CListView的加载性能提高80%

CListView是那样简单好用,以至于咱们爱它就好像老鼠爱大米一样。可是你是否知道它的控制类CListCtrl有很多雷区呢?尤其当Items非常大时(5000以上),对CListCtrl的用法不当,会导致CListView的加载明显延迟。来看看下面载入逗号分隔文本CSV的常见用法:

 


//Created By RAINEE,2006-3-8
void CRecordDoc::LoadFileCSV(CStdioFile& file_Open,
                                                  CListCtrl& openListCtrl, int& ReadNum)
{
       ReadNum = 0;
      
       int nSubItem = 0;
       int nStart = 0, nEnd = 0;
       CString strTempOpen;
       const TCHAR tcSplit = _T(',');
       while(1)
       {
              if(file_Open.ReadString(strTempOpen)==FALSE) break;
             
              for (nSubItem = nStart = 0; nSubItem < 11; nSubItem++)
              {
                     nEnd = strTempOpen.Find(tcSplit, nStart);
                     if (nEnd == -1)
                     {
                            break;
                     }
 
                     if (nSubItem == 0)
                     {
                            ReadNum ++;
                            openListCtrl.InsertItem(0, strTempOpen.Mid(nStart, nEnd - nStart));
                     }
                     else
                     {
                            openListCtrl.SetItemText(0, nSubItem, strTempOpen.Mid(nStart, nEnd - nStart));
                     }
 
                     nStart = nEnd + 1;
              }
       };
 }

 


 

看出问题了么?

优化1:巧用CListCtrl::SetRedraw(BOOL bRedraw)函数,性能提升75%

 


 

//Created By RAINEE,2006-3-8
void CRecordDoc::LoadFileCSV(CStdioFile& file_Open,
                                                  CListCtrl& openListCtrl, int& ReadNum)
{
       SetCursor(LoadCursor(NULL, IDC_WAIT));
       openListCtrl.SetRedraw(FALSE);
      
       ReadNum = 0;
      
      int nSubItem = 0;
       int nStart = 0, nEnd = 0;
       CString strTempOpen;
       const TCHAR tcSplit = _T(',');
       while(1)
       {
              if(file_Open.ReadString(strTempOpen)==FALSE) break;
             
              for (nSubItem = nStart = 0; nSubItem < 11; nSubItem++)
              {
                     nEnd = strTempOpen.Find(tcSplit, nStart);
                     if (nEnd == -1)
                     {
                            break;
                     }
 
                     if (nSubItem == 0)
                     {
                            ReadNum ++;
                            openListCtrl.InsertItem(0, strTempOpen.Mid(nStart, nEnd - nStart));
                     }
                     else
                     {
                            openListCtrl.SetItemText(0, nSubItem, strTempOpen.Mid(nStart, nEnd - nStart));
                     }
 
                     nStart = nEnd + 1;
              }
       };
      
       openListCtrl.SetRedraw(TRUE);
       SetCursor(LoadCursor(NULL, IDC_ARROW));
}

 


 

不卖关子了,接着来

优化2:新纪录项追加在List底部,性能提高35%


//Created By RAINEE,2006-3-8
void CRecordDoc::LoadFileCSV(CStdioFile& file_Open,
                                                  CListCtrl& openListCtrl, int& ReadNum)
{
        SetCursor(LoadCursor(NULL, IDC_WAIT));
       openListCtrl.SetRedraw(FALSE);
      
        ReadNum = 0;
      
       int nSubItem = 0;
       int nRootItem = -1;//插入项
       int nStart = 0, nEnd = 0;
       CString strTempOpen;
       const TCHAR tcSplit = _T(',');
       while(1)
       {
              if(file_Open.ReadString(strTempOpen)==FALSE) break;
             
              for (nSubItem = nStart = 0; nSubItem < 11; nSubItem++)
              {
                     nEnd = strTempOpen.Find(tcSplit, nStart);
                     if (nEnd == -1)
                     {
                            break;
                     }
 
                     if (nSubItem == 0)
                     {
                            nRootItem ++;
                            openListCtrl.InsertItem(nRootItem, strTempOpen.Mid(nStart, nEnd - nStart));
                     }
                     else
                     {
                            openListCtrl.SetItemText(nRootItem, nSubItem, strTempOpen.Mid(nStart, nEnd - nStart));
                     }
 
                     nStart = nEnd + 1;
              }
       };
       ReadNum = nRootItem + 1;
      
       openListCtrl.SetRedraw(TRUE);
       SetCursor(LoadCursor(NULL, IDC_ARROW));
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值