procedure TMainForm.LoadListViewToFile(AListView: TListView; sFileName:
string);
var
F: TFileStream;
IdxItem, IdxSubItem, IdxImage: Integer;
W, ItemCount, SubCount: Word;
pText: PChar;
PTemp: PChar;
MySignature:
array [0..2]
of Char;
sExeName:
string;
begin
with AListView
do
begin
ItemCount := 0;
SubCount := 0;
sExeName := ExtractFileName(sFileName);
if not FileExists(sFileName)
then
begin
MessageBox(
Handle, PChar(Format(Msg1, [sExeName])), '
I/O Error', MB_ICONERROR);
Exit;
end;
F := TFileStream.Create(sFileName, fmOpenRead);
F.
Read(MySignature, SizeOf(MySignature));
if MySignature <> '
LVF'
then
begin
MessageBox(
Handle, PChar(Format(Msg2, [sExeName])), '
I/O Error', MB_ICONERROR);
Exit;
end;
F.
Read(ItemCount, SizeOf(ItemCount));
Items.Clear;
for idxItem := 1
to ItemCount
do
begin
with Items.Add
do
begin
//Read imageindex
F.
Read(SubCount, SizeOf(SubCount));
//Read imageindex
F.
Read(IdxImage, SizeOf(IdxImage));
ImageIndex := IdxImage;
//Read the Caption
F.
Read(w, SizeOf(w));
pText := StrAlloc(w + 1);
pTemp := StrAlloc(w + 1);
F.
Read(pTemp^, W);
StrLCopy(pText, pTemp, W);
Caption := StrPas(pText);
StrDispose(pTemp);
StrDispose(pText);
if SubCount > 0
then
begin
for idxSubItem := 1
to SubCount
do
begin
F.
Read(w, SizeOf(w));
pText := StrAlloc(w + 1);
pTemp := StrAlloc(w + 1);
F.
Read(pTemp^, W);
StrLCopy(pText, pTemp, W);
Items[idxItem - 1].SubItems.Add(StrPas(pText));
StrDispose(pTemp);
StrDispose(pText);
end;
end;
end;
end;
F.Free;
end;
end;