Listbox OwnerDrawFixed weirdo

Anyone had any problems with a ListBox using OwnerDrawFixed? It seems that your carefully drawn LisBox item gets creamed when it becomes selected. In the OnDrawItem it is possible to tell if the ListBoxItem has been selected, but there seems to be no way of disabling what ListBox itself does to the selected item.

The programme is on
https://59b7770ca3fb18e4e90d-52e2682744779750b5103bbecf998085.ssl.cf2.rackcdn.com/ListBoxProblem.zip

Run it, then try clicking on the ListBox items!

Hi Chester,

Try setting the Font.Color instead of Pen.Color (see TCustomListBox.CNDrawItem in Vcl.StdCtrls).
Also check the selected state like:

if odSelected in State then
Font.Color := clBlack
else
Font.Color := clRed;

Cheers,
Cosmin

Many thanks, Cosmin! It looks a lot better now:

  Pen.Color := Colour;
  Pen.Style := psSolid;
  Pen.Width := 1;
  Brush.Style := bsSolid;
  Brush.Color := Colour;

  if odSelected in State then
  begin
     Font.Color := clGreen;
     Font.Style := [fsBold];
     Brush.Color := $E0E0FF;
  end
  else
  begin
     Font.Color := clBlack;
     Font.Style := [];
  end;
  Font.Size := 14;

  Rectangle(Rect);   //If you use FillRect here it changes the colour of the form, not the item!

  Pen.Color := clRed;
  Pen.Width := 2;
  MoveTo(0, Rect.Bottom - 1);
  LineTo(Rect.Right, Rect.Bottom - 1);

  Offset := 2;          { Provide default offset. }

  TextOut(Rect.Left + Offset, Rect.Top + Offset, ListBox1.Items[Index])  { Display the text. }
1 Like