CSDN博客

img xmlscript

数据量大时,ListView更新更快的方法

发表于2004/11/3 13:53:00  2423人阅读

分类: ·Win32 SDK 和 C++

 采用常规方法处理ListView,在数据量大时无论是还是用了BeginUpdata和EndUpdata()还是处理WM_EraseBkgnd信息都不会得到理想的效果,那答案是什么呢?
嗯,采用所谓Virtual ListView。
说起来很神秘,其实点通了很简单。
在于把ListView->OwnerData设为true,然后直接设置
ListView->Items->Count为数据总数,实际的数据可以保在任意其它速度较快且可直接索引的容器里(如STL中的 vector),然后关键是在OnData事件中写入代码通过Item->Index从容器中(如vector)取出数据,然后用
Item->Caption = ???;
Item->SubItems->Add(???);
Item->SubItems->Add(???);
来动态的写入数据到ListView用于UI显示,如此Virtual ListView即完成.
一个完整的简单例子如下:
/*Unit1.h*/
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <forms.hpp>
#include <ComCtrls.hpp>
#include <vector>
using namespace std;
//---------------------------------------------------------------------------
struct ListViewData
{
AnsiString Caption;
AnsiString SubItems1;
AnsiString SubItems2;
AnsiString SubItems3;
};
class Tform1 : public Tform
{
__published:// IDE-managed Components
TListView *ListView1;
void __fastcall ListView1Data(TObject *Sender, TListItem *Item);
private:// User declarations
vector<ListViewData*> m_lvDataVector;
public:// User declarations
__fastcall Tform1(TComponent* Owner);
__fastcall ~Tform1();
};
//---------------------------------------------------------------------------
extern PACKAGE Tform1 *form1;
//---------------------------------------------------------------------------
#endif

/*unit1.cpp*/
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tform1 *form1;
//---------------------------------------------------------------------------
__fastcall Tform1::Tform1(TComponent* Owner)
: Tform(Owner)
{
ListView1->OwnerData = true;
const int iListViewCount = 5000;
ListView1->Items->Count = iListViewCount;
for(int i = 0; i < iListViewCount; ++i) {
ListViewData *plvData = new ListViewData;
plvData->Caption = "Caption" + AnsiString(i);
plvData->SubItems1 = "SubItems1" + AnsiString(i);
plvData->SubItems2 = "SubItems2" + AnsiString(i);
plvData->SubItems3 = "SubItems3" + AnsiString(i);
m_lvDataVector.push_back(plvData);
}
}

__fastcall Tform1::~Tform1()
{
int iCount = ListView1->Items->Count;
for(int i = 0; i < iCount; ++i) {
ListViewData *plvData = m_lvDataVector[i];
delete plvData;
}
}
//---------------------------------------------------------------------------

void __fastcall Tform1::ListView1Data(TObject *Sender, TListItem *Item)
{
ListViewData *plvData = m_lvDataVector[Item->Index];
Item->Caption = plvData->Caption;
Item->SubItems->Add(plvData->SubItems1);
Item->SubItems->Add(plvData->SubItems2);
Item->SubItems->Add(plvData->SubItems3);
}
//---------------------------------------------------------------------------


btw,cker指点:
我现在觉得tlistview 和 tTreeView比较烂。
最近正在熟悉一个号称最好的Tree和list控件,非常的棒。开放源码,
www.delphi-gems.com的virtual trees
同样是使用所谓的Virtual技术....,这个控件是我接触BCB和Delphi以来见过的最好的控件。
阅读全文
0 0

相关文章推荐

img
取 消
img