用了begin update和End update语句,只是解决大量输入数据时屏幕不刷新,对于整体性能
来说,提高不大!你记住我这句话:要想从根本上解决添加海量数据时的效率问题,最关键
的是不能一次性的将数据全都装入,其他边边角角的小优化都不起决定性的作用!
下面是我的一段代码,实际上就是做了一个Windows资源管理器右边的文件列表,最主要的
部分如下:
void __fastcall TFrmMain::LvwFileListData(TObject *Sender, TListItem *Item)
{
ItemStruct* OneItem;
if( Item->Index < FileList->Count )
{
OneItem = GetItem(Item->Index);
Item->Caption = OneItem->strName; //文件名
Item->ImageIndex = OneItem->ImageIndex; //图标索引
Item->SubItems->Add( OneItem->strNote ); //注释 //??
Item->SubItems->Add( FormatFloat("#,###0' KB'", OneItem->size/1024) ); //大小
Item->SubItems->Add( OneItem->strType ); //类型
Item->SubItems->Add( FileDateToDateTime(OneItem->time) ); //时间
}
}
//---------------------------------------------------------------------------
void __fastcall TFrmMain::LvwFileListDataHint(TObject *Sender,
int StartIndex, int EndIndex)
{
if( (StartIndex < FileList->Count) && (EndIndex < FileList->Count) )
HardJob(StartIndex, EndIndex);
}
//---------------------------------------------------------------------------
void __fastcall TFrmMain::FormCreate(TObject *Sender)
{
//。。。。。。。。。。。。。。。。。。。。
/*----------取得文件关联图标的列表,并指向TListView控件----------*/
SHFILEINFO FileInfo;
THandle ImageListHandle;
ImageListHandle = SHGetFileInfo("", 0, &FileInfo, sizeof(FileInfo),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
SendMessage(LvwFileList->Handle, LVM_SETIMAGELIST, LVSIL_SMALL, ImageListHandle);
}
//---------------------------------------------------------------------------
void __fastcall TFrmMain::GotoPath(AnsiString CurrentPath)
{
//在设置路径的开始,就进行数据库的相关查找,此时还没到文件搜索的时候
TCursor SaveCursor;
ItemStruct* OneItem;
TSearchRec sr;
int iFound;
SaveCursor = Screen->Cursor;
LvwFileList->Items->BeginUpdate();
try
{
Screen->Cursor = crHourGlass;
ClearFileList();
iFound = FindFirst(CurrentPath + "*.*", faAnyFile, sr);
while( iFound == 0 )
{
if( (sr.Name != ".") && (sr.Name != "..") && ((sr.Attr & faDirectory) != faDirectory) )
{
OneItem = new ItemStruct;
OneItem->strName = sr.Name; //文件名
OneItem->size = sr.Size; //大小
OneItem->time = sr.Time; //日期
FileList->Add(OneItem);
}
iFound = FindNext(sr);
}
FindClose(sr);
LvwFileList->Items->Count = FileList->Count;
// LvwFileList->Repaint();
}
__finally
{
Screen->Cursor = SaveCursor;
LvwFileList->Items->EndUpdate();
}
}
//---------------------------------------------------------------------------
void __fastcall TFrmMain::HardJob(int StartIndex, int EndIndex)
{ //只有在需要的时候,本函数才会被调用,这极大的提升了填充文件列表的速度
AnsiString FileWholeName = "";
TSHFileInfo FileInfo;
int i;
for(i=StartIndex; i<=EndIndex; i++)
{
//此处加入注释
for(int j=0; j<NoteFileList->Count; j++)
{
if(CompareText(NoteFileList->Strings[j], GetItem(i)->strName) == 0)
{
GetItem(i)->strNote = NoteList->Strings[j];
break;
}
}
// FileWholeName = CurrentPath + GetItem(i)->strName;
FileWholeName = CurrentPath + GetItem(i)->strName;
SHGetFileInfo(FileWholeName.c_str(), 0, &FileInfo, sizeof(FileInfo),
SHGFI_TYPENAME | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
GetItem(i)->strType = FileInfo.szTypeName; //文件类型
GetItem(i)->ImageIndex = FileInfo.iIcon; //文件图标索引
}
}
//---------------------------------------------------------------------------
你要想用好ListView,就要仔细研究一下Delphi或C++Builder自带的VirtualListView例子