How do I draw the selected list-box item in a different color?

前端 未结 3 1465
北恋
北恋 2020-12-11 15:38

Is is possible to change the item selection focus color and text color in a TListBox?

When themes are not enabled in the project, or the list box style is set to own

相关标签:
3条回答
  • 2020-12-11 16:06

    This helped me do something I needed to do also, namely, eliminate any visible selection. I modified the code above very slightly to accomplish this:

    procedure TForm1.OnDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      with (Control as TListBox).Canvas do
      begin
        if odSelected in State then
        begin
          Brush.Color := clWhite;
          Font.Color := clBlack;
        end;
    
        FillRect(Rect);
        TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
        if odFocused In State then begin
          Brush.Color := ListBox1.Color;
          DrawFocusRect(Rect);
        end;
      end;
    end;
    

    Made the selected item's background color white, and it's font color black, which did what I needed. Thanks so much!

    0 讨论(0)
  • 2020-12-11 16:08

    I saw, Style property has to be lbOwnerDrawFixed

    0 讨论(0)
  • 2020-12-11 16:10

    try this:

    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      with (Control as TListBox).Canvas do
      begin
        if odSelected in State then
          Brush.Color := $00FFD2A6;
    
        FillRect(Rect);
        TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
        if odFocused In State then begin
          Brush.Color := ListBox1.Color;
          DrawFocusRect(Rect);
        end;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题