如何使listbox的所选项的背景色去掉,而所选 项的字体变成红色,现在所选项默认的是蓝色背景和白色字体

解决方案 »

  1.   

    呵呵,把你的listbox的style改成lbOwnerDrawVariable,然后响应OnDrawItem事件。
    假设正常情况下背景为clBackground(蓝色)。
    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
      with (Control as TListBox).Canvas do  { draw on control canvas, not on the form }
    begin
        if ListBox1.Selected[Index] then
        begin
          Brush.Color := clWhite;
          Font.Color := clRed;
        end
        else
        begin
          Brush.Color := clBackground;
          Font.Color := clWhite;
        end;    FillRect( Rect );
        TextOut(Rect.Left + 2, Rect.Top, (Control as TListBox).Items[Index])
      end
    end;另响应OnClick:
    procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      ListBox1.Invalidate;
    end;