Hola a todos de nuevo, sigo probando cosas con los listview y me surgen algunos problemillas. En principio, puedo agregar correctamente nuevos elementos, pero cuando se cargan desde un archivo, se trunca la longitud del texto por lo que no aparece con la misma cantidad de caracteres que tenían cuando se agregaron los elementos.
Adjunto dos imagenes, en la primera se muestra todo el texto completo (ojo, fijaros en el último elemento del listview). En la segunda imagen, se ve como se cortan las cadenas de texto.
Otra cosa es que en la longitud del listview, uso "-1" para que el ancho de las columnas sea igual que la longitud del texto.
IMAGEN 1
IMAGEN 2
El código que guarda los elementos del listview, lo encontre buscando información por la web y es este (por si sive de ayuda):
Código Delphi
[-]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 Lista do
begin
ItemCount := 0;
SubCount := 0;
MySignature := 'LVF';
F := TFileStream.Create(save.FileName, fmCreate or fmOpenWrite);
F.Write(MySignature, SizeOf(MySignature));
if Items.Count = 0 then
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
if SubItems.Count = 0 then
SubCount := 0
else
SubCount := Subitems.Count;
F.Write(SubCount, SizeOf(SubCount));
IdxImage := ImageIndex;
F.Write(IdxImage, SizeOf(IdxImage));
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
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;
Y el código que carga los elementos desde un archivo este:
Código Delphi
[-]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('registro.cvs');
if not FileExists('registro.cvs') then
begin
MessageBox(Handle, PChar(Format(Msg1, [sExeName])), 'I/O Error', MB_ICONERROR);
Exit;
end;
F := TFileStream.Create('registro.cvs', 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
F.Read(SubCount, SizeOf(SubCount));
F.Read(IdxImage, SizeOf(IdxImage));
ImageIndex := IdxImage;
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;
Luego en un botón hace la llamada al procedimiento guardar datos del listview y en el evento oncreate, los carga al iniciar el programa.
¿Alguien puede ayudarme? ¿alguna sugerencia?
Un saludo y gracias.