I am trying to ownerdraw a TListbox control but I cannot seem to get it to work or look right, maybe I am missing something obvious I don't know.
Please see the quick example below:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
begin
with ListBox1.Canvas do
begin
if (odSelected in State) then
begin
Brush.Color := clCream;
Pen.Color := clGreen;
FillRect(ARect);
Rectangle(ARect);
DrawFocusRect(ARect);
end
else
begin
Brush.Color := ListBox1.Color;
Pen.Color := ListBox1.Color;
FillRect(ARect);
end;
Font.Color := clWindowText;
TextOut(ARect.Left, ARect.Top, ListBox1.Items[Index]);
end;
end;
The problem is the rectangle around the selected item shows a partially dotted focus instead of a solid pen line. If I remove the DrawFocusRect(ARect) line then this issue still occurs only now the pen color seems to become purple.
I am using Lazarus v1.6, Windows 10, I have also set the Style property of the listbox to lbOwnerDrawVariable.
What am I doing wrong? I just need to color selected items with a custom fill and border color but couldnt figure out why I keep running into various graphical problems with how the items are drawn.
Thanks.