procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if odSelected in state then begin listbox1.Canvas.Brush.Color:=clred; ListBox1.Canvas.FillRect(rect); ListBox1.Canvas.TextOut(rect.Left+3,rect.Top+3,ListBox1.Items[index]); end else if (index mod 2)=1 then listbox1.Canvas.Brush.Color:=clblue else listbox1.Canvas.Brush.Color:=clwhite; ListBox1.Canvas.FillRect(rect); ListBox1.Canvas.TextOut(rect.Left+3,rect.Top+3,ListBox1.Items[index]); end;
第二步,
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
listbox1.Canvas.Brush.Color:=clyellow;//当然我这儿设置的是黄色,你也可以换成其它颜色
ListBox1.Canvas.FillRect(rect);
ListBox1.Canvas.TextOut(rect.Left+3,rect.Top+3,ListBox1.Items[index]);
end;
Rect: TRect; State: TOwnerDrawState);
begin
if odSelected in state then
begin
listbox1.Canvas.Brush.Color:=clred;
ListBox1.Canvas.FillRect(rect);
ListBox1.Canvas.TextOut(rect.Left+3,rect.Top+3,ListBox1.Items[index]);
end else
if (index mod 2)=1 then
listbox1.Canvas.Brush.Color:=clblue
else
listbox1.Canvas.Brush.Color:=clwhite;
ListBox1.Canvas.FillRect(rect);
ListBox1.Canvas.TextOut(rect.Left+3,rect.Top+3,ListBox1.Items[index]);
end;
Demos\DelphiWin32\VCLWin32\CustomDraw