Einzelnen Beitrag anzeigen

Popov
(Gast)

n/a Beiträge
 
#16

AW: Item in DrawItem ändern?

  Alt 21. Jun 2014, 16:36
Gehen, geht alles, nur wie sinnvoll ist das?
Delphi-Quellcode:
type
  TTest = class
    Text: String;
  end;

procedure AddItem(LB: TStrings; Str: String);
var
  Test: TTest;
begin
  Test := TTest.Create;
  Test.Text := Str;
  LB.AddObject('', Test);
end;

procedure ModItem(LB: TStrings; AIndex: Integer; Str: String);
begin
  if (AIndex >= 0) and (AIndex < LB.Count) then
    TTest(LB.Objects[AIndex]).Text := Str;
end;

function ItemStr(LB: TStrings; AIndex: Integer): String;
begin
  Result := TTest(LB.Objects[AIndex]).Text;
end;

procedure DelItem(LB: TStrings; AIndex: Integer);
begin
  if (AIndex >= 0) and (AIndex < LB.Count) then
    TTest(LB.Objects[AIndex]).Free;
end;

procedure GetFiles(Path: String; List: TStrings);
var
  Search: TSearchRec;
begin
  //Path := IncludeTrailingBackslash(Path);
  if FindFirst(Path + '*.*', faAnyFile, Search) = 0 then
  repeat
    AddItem(List, Path + Search.Name);
  until FindNext(Search) <> 0;
  FindClose(Search);
end;

procedure TForm1.FormCreate(Sender: TObject);
const
  Path = 'c:\';
begin
  ListBox1.Style := lbOwnerDrawFixed;
  ListBox1.Sorted := True;
  GetFiles(Path, ListBox1.Items);
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
const
  PfadeVerstecken = True; //<<<<
var
  FileName: String;
begin
  with (Control as TListbox) do
  begin
    if PfadeVerstecken then
      FileName := ExtractFileName(ItemStr(Items, Index))
    else
      FileName := ItemStr(Items, Index);

    Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top, FileName);

    ModItem(Items, Index, '[Mattze] ' + FileName); //<<<<<<<<<<<<<
  end;
end;

procedure TForm1.RefreshBtnClick(Sender: TObject);
begin
  ListBox1.Refresh;
end;
Also hier kann man den Inhalt ändern, ohne Endlosschleife, weil nicht der Item-String genutzt wird, sondern der String im Objekt abgelegt ist. Dadurch merkt die ListBox nicht sofort, wenn sich der Inhalt ändert.

Trotzdem, etwas sinnvolles wird nichts draus, da bei jeder Aktualisierungs die Änderung vorgenommen wird.
  Mit Zitat antworten Zitat