呵呵,把你的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;
假设正常情况下背景为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;