我的问题和这个基本一样question .但是,我想让颜色从设置的颜色从左到右流动到白色。我的想法是,我想将每个项目“填充”到 100%,然后逐渐将颜色从绿色变为黄色再变为红色。
最佳答案
试试这段代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
public
{ Public declarations }
procedure AddLog(const aStr : String; const aColor : TColor);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AddLog(const aStr: String; const aColor: TColor);
begin
ListBox1.Items.AddObject(aStr, TObject(aColor));
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
OldColor : TColor;
begin
with ListBox1.Canvas do begin
OldColor := Font.Color;
Font.Color := TColor( ListBox1.Items.Objects[Index] );
TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
Font.Color := OldColor;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Randomize;
AddLog(
'String #' + IntToStr(ListBox1.Items.Count),
RGB(Random(11) * 20 , Random(11) * 20, Random(11) * 20)
);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Clear;
end;
end.
关于delphi - 如何以不同的颜色绘制列表框中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20991434/