//设置listbox.style=lbOwnerDrawFixed procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if index mod 2 =0 then TListBox(Control).Canvas.Brush.Color:=clREd else TListBox(Control).Canvas.Brush.Color:=clBlue; TListBox(Control).Canvas.TextRect(Rect,rect.Left,Rect.Top,TListBox(Control).Items[index]);end;
//补充楼上的 type TListBoxEx = class(TListBox);procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if Odd(Index) then TListBox(Control).Canvas.Brush.Color := clRed else TListBox(Control).Canvas.Brush.Color := clBlue; TListBoxEx(Control).OnDrawItem := nil; TListBoxEx(Control).DrawItem(Index, Rect, State); TListBoxEx(Control).OnDrawItem := ListBox1DrawItem; end;
Rect: TRect; State: TOwnerDrawState);
begin
if index mod 2 =0 then
TListBox(Control).Canvas.Brush.Color:=clREd
else
TListBox(Control).Canvas.Brush.Color:=clBlue;
TListBox(Control).Canvas.TextRect(Rect,rect.Left,Rect.Top,TListBox(Control).Items[index]);end;
type
TListBoxEx = class(TListBox);procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if Odd(Index) then
TListBox(Control).Canvas.Brush.Color := clRed
else TListBox(Control).Canvas.Brush.Color := clBlue;
TListBoxEx(Control).OnDrawItem := nil;
TListBoxEx(Control).DrawItem(Index, Rect, State);
TListBoxEx(Control).OnDrawItem := ListBox1DrawItem;
end;