procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var i: integer; dx, x: real; begin with Control as TListbox do begin Canvas.FillRect(Rect); //填充以前画的内容 if not (odSelected in State) then //是否被选中 begin if (Index mod 2) = 0 then begin Canvas.Brush.Color := $00FFECD9; Canvas.FillRect(Rect); //填充以前画的内容 end end else if odFocused in State then //控件是否被激活 begin dx := (Rect.Right - Rect.Left) / 256; x := Rect.Left; for i := 0 to 255 do begin canvas.brush.color := RGB(i, 255, i); canvas.fillrect(classes.rect(round(x), Rect.Top, round(x + dx), Rect.Bottom)); x := x + dx; end; end else begin dx := (Rect.Right - Rect.Left) / 256; x := Rect.Left; for i := 0 to 255 do begin canvas.brush.color := RGB(i, i, i); canvas.fillrect(classes.rect(round(x), Rect.Top, round(x + dx), Rect.Bottom)); x := x + dx; end; end; Canvas.Brush.Style := bsClear;//显示当前Item的文字 Canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);//如果要处理状态为激活或选中时的样子,请不要取消下面的代码 // if (odFocused in State) and (odSelected in State) then // Canvas.DrawFocusRect(Rect); // Canvas.ClipRect); // end; end;procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Style := lbOwnerDrawFixed; end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
i: integer;
dx, x: real;
begin
with Control as TListbox do
begin
Canvas.FillRect(Rect); //填充以前画的内容 if not (odSelected in State) then //是否被选中
begin
if (Index mod 2) = 0 then
begin
Canvas.Brush.Color := $00FFECD9;
Canvas.FillRect(Rect); //填充以前画的内容
end
end
else
if odFocused in State then //控件是否被激活
begin
dx := (Rect.Right - Rect.Left) / 256;
x := Rect.Left;
for i := 0 to 255 do
begin
canvas.brush.color := RGB(i, 255, i);
canvas.fillrect(classes.rect(round(x), Rect.Top, round(x + dx), Rect.Bottom));
x := x + dx;
end;
end
else
begin
dx := (Rect.Right - Rect.Left) / 256;
x := Rect.Left;
for i := 0 to 255 do
begin
canvas.brush.color := RGB(i, i, i);
canvas.fillrect(classes.rect(round(x), Rect.Top, round(x + dx), Rect.Bottom));
x := x + dx;
end; end; Canvas.Brush.Style := bsClear;//显示当前Item的文字
Canvas.TextOut(Rect.Left, Rect.Top,
ListBox1.Items[Index]);//如果要处理状态为激活或选中时的样子,请不要取消下面的代码
// if (odFocused in State) and (odSelected in State) then
// Canvas.DrawFocusRect(Rect); // Canvas.ClipRect); //
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawFixed;
end;