Enregistrer/charger un TListView

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, comctrls, StdCtrls; type TForm1 = class (TForm) ListView1: TListView; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private procedure SaveListViewToFile(AListView: TListView; sFileName: string); procedure LoadListViewToFile(AListView: TListView; sFileName: string); public end; const Msg1 = 'File "%s" does not exist!'; Msg2 = '"%s" is not a ListView file!'; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.SaveListViewToFile(AListView: TListView; sFileName: string); var idxItem, idxSub, IdxImage: integer; F: TFileStream; pText: PChar; sText: string; W, ItemCount, SubCount: word; MySignature: array [0..2] of char; begin with AListView do begin ItemCount := 0; SubCount := 0; file://**** MySignature := 'LVF'; // ListViewFile F := TFileStream.Create(sFileName, fmCreate or fmOpenWrite); F.Write(MySignature, sizeof(MySignature)); if Items.Count = 0 then // List is empty ItemCount := 0 else ItemCount := Items.Count; F.Write(ItemCount, Sizeof(ItemCount)); if Items.Count > 0 then begin for idxItem := 1 to ItemCount do begin with items[idxItem - 1] do begin file://Save subitems count if SubItems.Count = 0 then SubCount := 0 else SubCount := Subitems.Count; F.Write(SubCount, Sizeof(SubCount)); file://Save ImageIndex IdxImage := ImageIndex; F.Write(IdxImage, Sizeof(IdxImage)); file://Save Caption sText := Caption; w := length(sText); pText := StrAlloc(Length(sText) + 1); StrPLCopy(pText, sText, Length(sText)); F.Write(w, sizeof(w)); F.Write(pText^, w); StrDispose(pText); if SubCount > 0 then begin for idxSub := 0 to SubItems.Count - 1 do begin file://Save Item's subitems sText := SubItems[idxSub]; w := length(sText); pText := StrAlloc(Length(sText) + 1); StrPLCopy(pText, sText, Length(sText)); F.Write(w, sizeof(w)); F.Write(pText^, w); StrDispose(pText); end; end; end; end; end; F.Free; end; end; procedure TForm1.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 file://Read imageindex F.Read(SubCount, sizeof(SubCount)); file://Read imageindex F.Read(IdxImage, sizeof(IdxImage)); ImageIndex := IdxImage; file://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; // Example: procedure TForm1.Button1Click(Sender: TObject); begin // Save Items and Clear the ListView SaveListViewToFile(ListView1, 'MyListView.sav'); ListView1.Items.Clear; end; procedure TForm1.Button2Click(Sender: TObject); begin // Load Items LoadListViewToFile(ListView1, 'MyListView.sav'); end; ', '', '', '', NULL);
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour30/11/2001
VisualisationVu 941 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Objet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur