3

My question is basically the same as this question. However, I want make the color flow from left to right from a set color to white. The idea is that I want to "fill" every item to 100% and that gradually changes the color from green to yellow to red.

Community
  • 1
  • 1
Eszee
  • 264
  • 2
  • 14
  • For a simple procedure that does gradient fill, see [`How to Draw a Gradient Fill on a Canvas`](http://delphi.about.com/od/adptips2006/qt/gradient_fill.htm). You also have `GradientFillCanvas` in `GraphUtil.pas`. – LU RD Jan 08 '14 at 10:02
  • thats great m8. Gonna learn this tnx – Eszee Jan 08 '14 at 10:56
  • And along with @LURD's tip, there's code to custom draw ListBox items differently [here]; if you combine the two, you should have your solution. :-) – Ken White Jan 08 '14 at 12:04
  • @KenWhite, where?. See also [`How to create gradient colour schemes`](http://www.delphidabbler.com/tips/139) how to work out a color that varies between three colors by percentage. – LU RD Jan 08 '14 at 12:27
  • @LURD: Oops! Posted the comment on the way out the door, and apparently forgot to leave the link; just found it in my jacket pocket. :-) I was referring to [Tabs and colored lines in ListBox](http://stackoverflow.com/a/18091990/62576). – Ken White Jan 08 '14 at 13:47

1 Answers1

1

Try this code:

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.
huxahetu
  • 193
  • 5
  • The OP wants listbox item background filled with a gradient. End color is white and start color varies from green to yellow to red in a gradient scale from 0 to 100 %. – LU RD Jan 08 '14 at 23:13
  • You need to change default ListBox style from lbStandard to lbOwnerDrawFixed, or lbOwnerDrawVariable. `ListBox1.Style := lbOwnerDrawFixed;` – user1158332 Jan 26 '22 at 13:54