2014-01-08 77 views
1

我的问题与此基本相同question。但是,我想让颜色从左到右从设置的颜色流向白色。这个想法是,我想“填充”每个项目100%,逐渐将颜色从绿色变为黄色变为红色。如何在不同颜色的列表框中绘制项目

+0

对于一个简单的过程,做渐变填充,请参阅['如何绘制渐变上Canvas'填充](http://delphi.about.com/od/ adptips2006/QT/gradient_fill.htm)。 'GraphUtil.pas'中也有'GradientFillCanvas'。 –

+0

这就是伟大的M8。要学习这个tnx – Eszee

+0

并且@LURD的提示,还有代码来自定义绘制ListBox项目[这里];如果你将这两者结合起来,你应该有你的解决方案。 :-) –

回答

0

试试这个代码:

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. 
+0

OP希望列表框项目背景充满渐变。最终颜色为白色,开始颜色从绿色变为黄色变为红色,渐变色阶从0到100%。 –

+0

@LURD请看......谢谢! – huxahetu

相关问题