2014-01-08 76 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

相關問題