type
TFBColor = record //class(TObject)
foreground: TColor;
background: TColor;
styling: TFontStyles;
end;
TFBColors = array[0..255] of ^TFBColor;
{ TListboxHelper }
TListboxHelper = class helper for TListbox
procedure AddElement(const Element: String; const AColor, BColor: TColor; FStyle: TFontStyles = []);
procedure RemoveElement(Index: Integer);
function GetItemColors(Index: Integer): TFBColor;
end;
// global variables
var
Form1: TForm1;
FBColors: TFBColors;
PFBColor: ^TFBColor;
{$R *.lfm}
implementation
{ TListboxHelper }
procedure TListboxHelper.AddElement(const Element: String; const AColor, BColor: TColor; FStyle: TFontStyles = []);
var
idx: Integer = 0;
begin
New(PFBColor);
idx := Items.count;
PFBColor^.foreground := AColor;
PFBColor^.background := BColor;
PFBColor^.styling:= FStyle;
FBColors[idx] := PFBColor;
AddItem(Element, TObject(PtrUint(FBColors[idx])));
end;
procedure TListboxHelper.RemoveElement(Index: Integer);
begin
Items.Delete(Index);
Dispose(FBColors[Index]);
end;
function TListboxHelper.GetItemColors(Index: Integer): TFBColor;
begin
Result := TFBColor(FBColors[Index]^);
end;
// then the draw function
procedure TForm1.ElementListBoxDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
const
Offset = 2;
AdjOffset = 3;
ColorWidth = 28;
Tab = 5;
var
rbox: TRect;
ItemFontSize: Integer;
FillColor, PenColor, TextColor: TColor;
Colors: TFBColor;
Cnvs: TCanvas;
ListBox: TListBox;
DispText: string[5] = ' abc ';
begin
if Index < 0 then
Exit;
ListBox := Control as TListBox;
Cnvs := ListBox.Canvas;
Colors := FBColors[Index]^;
rbox.top := ARect.top + Offset;
rbox.bottom := ARect.bottom - Offset;
rbox.left := ARect.left + AdjOffset;
rbox.right := rbox.left + ColorWidth;
if not(odBackgroundPainted in State) then
Cnvs.FillRect(rbox);
PenColor := Cnvs.Pen.Color;
FillColor := ListBox.Color;
Cnvs.Brush.Color := Colors.background;
Cnvs.Pen.Color := silver;
Cnvs.Font.Color := Colors.foreground;
Cnvs.Rectangle(BidiFlipRect(rbox, ARect, UseRightToLeftAlignment));
ItemFontSize := Canvas.Font.Size;
Cnvs.Font.Size:= 8;
Cnvs.Font.Style:=Colors.Styling;
Cnvs.TextOut(rbox.Left + 1, rbox.Top + AdjOffset, DispText);
Cnvs.Brush.Color := FillColor;
Cnvs.Pen.Color := PenColor;
Cnvs.Font.Color := clWindowText;
Cnvs.Font.Size := ItemFontSize;
Cnvs.Font.Style:= [];
rbox := ARect;
rbox.left := rbox.left + ColorWidth + Offset + Tab;
Cnvs.TextOut(rbox.Left, rbox.Top + Offset, ElementListBox.Items[Index]);
end;