delphi - 如何以不同的颜色绘制列表框中的项目

标签 delphi listbox

我的问题和这个基本一样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/

相关文章:

c# - 在 C# 中填充列表框

c# - WPF 绑定(bind)到资源中的元素

xml - Delphi解析xml文件

delphi - 如何在使用 Delphi XE2 进行 iPhone 开发的 Free Pascal 中包含 Objective C 文件

delphi - 是否可以将属性连接到嵌入式组件

delphi - 无法在 Delphi 7 的控制台应用程序中使用随机方法

python - 更改 Tkinter 列表框中项目的颜色

wpf - 将禁用的列表框背景更改为灰色

delphi - 如何在 Delphi 中递归创建文件夹?

wpf - 在 ListBox 中查找控件?