H
han10
Unregistered / Unconfirmed
GUEST, unregistred user!
想要TListBox的不同行,显示不同的颜色,可是运行后只有第一行绘制正确,如果拉动滚动条,其他的行才出来Item的内容,为什么,以下是源码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if Index mod 2 =0 then
begin
with ListBox1.Canvas do
begin
brush.Color := clRed;
FillRect(rect);
Font.Color := clBlack;
TextRect(rect,2,0,ListBox1.items[index]+'ddd');
end;
end
else
begin
with ListBox1.Canvas do
begin
brush.Color := clSkyblue;
FillRect(rect);
Font.Color := clBlack;
TextRect(rect,2,0,ListBox1.items[index]);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
for i:=0 to 20 do
begin
ListBox1.Items.Add('错误信息');
ListBox1.Items.Add(inttostr(i)+'正确信息...');
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if Index mod 2 =0 then
begin
with ListBox1.Canvas do
begin
brush.Color := clRed;
FillRect(rect);
Font.Color := clBlack;
TextRect(rect,2,0,ListBox1.items[index]+'ddd');
end;
end
else
begin
with ListBox1.Canvas do
begin
brush.Color := clSkyblue;
FillRect(rect);
Font.Color := clBlack;
TextRect(rect,2,0,ListBox1.items[index]);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
for i:=0 to 20 do
begin
ListBox1.Items.Add('错误信息');
ListBox1.Items.Add(inttostr(i)+'正确信息...');
end;
end;
end.