Hi,
ich hab's mal so gelöst, dass ich über drei Buttons einem markierten Item in der Listbox eine andere Textfarbe zuordne. Die Listbox musste im Style dazu auf - so glaube ich zumindest - lbOwnerDrawFixed gesetzt werden (Sorry, aber ich habe gerade kein Delphi zur Hand, daher nur so aus dem Hinterkopf).
Als erstes das Ereignis OnDrawItem der Listbox verarzten:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
| procedure TMain.LBFilesDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var Farbe:TColor; begin with Control as TListBox do begin Canvas.FillRect(Rect); Farbe:=TColor(Items.Objects[Index]); if Farbe=clBlack then begin if Selected[Index] then Farbe:=clWhite; end else if Farbe=clWhite then begin if not Selected[Index] then Farbe:=clBlack; end; Canvas.Font.Color:=Farbe; Canvas.TextOut(Rect.Left+2,Rect.Top,Items[Index]); end; end; |
Dann noch die Buttons zum markieren (sie unterscheiden sich über Tag):
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
| procedure TMain.BtnMarkierenClick(Sender: TObject); var i,t,tx:Integer; Merke:string; begin t:=(Sender as TComponent).Tag; for i:=0 to Listbox.Items.Count-1 do begin if Listbox.Selected[i] then begin Merke:=Listbox.Items[i]; Listbox.Items.Delete(i); if t=1 then Listbox.Items.InsertObject(i,Merke,Pointer(clBlue)) else if t=2 then begin Listbox.Items.InsertObject(i,Merke,Pointer(clRed)) else Listbox.Items.InsertObject(i,Merke,Pointer(clFuchsia)); end; end; Listbox.ItemIndex:=Listbox.Items.IndexOf(Merke); Listbox.SetFocus; end; |
Fehlt nur noch der umgekehrte Fall, also ist schon Item ist schon rot und soll wieder schwarz werden. Das dürfte aber auch kein Problem sein.
Ich hoffe, ich habe nichts vergessen.
Gruß aus dem Norden
blaskito