How to change the default highlight color of a TListBox (Views: 1213)
Problem/Question/Abstract: Can anyone tell me how to change the default highlight color used in a TListBox? I need it to be clAqua instead of the standard Navy as the text in the listbox is made other colors in the OwnerDraw and you can't read some of them with the Navy selection color. Answer: Solve 1: Check the 'State' parameter in the DrawItem event. It lets you know if the item is selected. If it is then use a different brush color. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if odSelected in State then ListBox1.Canvas.Brush.Color := clAqua; ListBox1.Canvas.FillRect(Rect); ListBox1.Canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]); end; Solve 2: Set Style := lbOwnerDrawFixed and OnDrawItem := ListBoxDrawItem; . Remove the last line from the example if you want to have the focus rectangle. procedure TListBox.ListBoxDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if (odSelected in State) then Canvas.Brush.Color := clBlue else Canvas.Brush.Color := Color; Canvas.FillRect(Rect); Canvas.Font := Font; SetTextAlign(Canvas.Handle, TA_LEFT or TA_TOP or TA_NOUPDATECP); ExtTextOut(Canvas.Handle, Rect.Left + 2, Rect.Top + 1, ETO_CLIPPED or ETO_OPAQUE, @Rect,PChar(Items[Index]), Length(Items[Index]), nil); if (odSelected in State) then DrawFocusRect(Canvas.Handle, Rect); end; |