Ver Mensaje Individual
  #6  
Antiguo 29-11-2014
Avatar de danielmj
danielmj danielmj is offline
Miembro
 
Registrado: jun 2011
Posts: 383
Reputación: 13
danielmj Va por buen camino
hola buenos dias.

perdona que no haya contestado antes pero no sé por que extraña razón, no me ha llegado ninguna notificación a la bandeja del correo. En cuanto a lo que comentas ecfisa, ahora me pongo con ello, aún así a continuación pongo el código tal como lo tengo yo.

Código Delphi [-]
procedure TForm1.SaveLista(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
  // Inicio
  with Lista do
  begin
    ItemCount := 0;
    SubCount  := 0;
    //****
    MySignature := 'cl';
    //  ListViewFile
    F := TFileStream.Create('clvs.cl', 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
          // Guardamos los SubItems
          if SubItems.Count = 0 then
            SubCount := 0
          else
            SubCount := Subitems.Count;
          F.Write(SubCount, SizeOf(SubCount));
          // Guardamos el Index
          IdxImage := ImageIndex;
          F.Write(IdxImage, SizeOf(IdxImage));
          // Guardamos la 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
              // Guardamos los Items y 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
  MySignature:= 'cl';
  with AListView do
  begin
    ItemCount := 0;
    SubCount  := 0;

    sExeName := ExtractFileName('clvs.cl');

    if not FileExists('clvs.cl') then
    begin
      MessageBox(Handle, PChar(Format(Msg1, [sExeName])), 'I/O Error', MB_ICONERROR);
      Exit;
    end;

    F := TFileStream.Create('clvs.cl', fmOpenRead);
    F.Read(MySignature, SizeOf(MySignature));

    if MySignature <> 'cl' 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
        // Leemos el Index
        F.Read(SubCount, SizeOf(SubCount));
        // Re Leemos el Index
        F.Read(IdxImage, SizeOf(IdxImage));
        ImageIndex := IdxImage;
        // Leemos el 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;

Siendo las llamadas a estos procedimientos...
Código Delphi [-]
procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.Text:= '';
  LoadListViewToFile(Lista, 'clvs.cl');
end;

Código Delphi [-]
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  SaveLista(Lista, 'clvs.cl');
  Lista.Items.Clear;
end;

Esta ultima llamada también la tengo en el OnClose del formulario. Tal como está ahora, me guarda un archivo tal como yo quiero (nombre y extensión) pero el contenido del archivo es "FLV" por lo que no tiene nada que ver con la lista.

Bueno, voy a mirar la web de torrys a ver que tal.
Un saludo y gracias.

Edito: Si mal no veo, el código que he puesto antes es igual que el de la web de torrys, salvando las diferencias en cuanto a nombre de archivo y extensión y un "if" dentro de la carga del archivo, por lo que sigo igual. No sé por que dentro del archivo escribe "LVF"
__________________
La juventud pasa, la inmadurez se supera, la ignorancia se cura con la educación, y la embriaguez con la sobriedad, pero la estupidez dura para siempre. Aristofanes.

Última edición por danielmj fecha: 29-11-2014 a las 12:07:57.
Responder Con Cita