1
2 unit Unit1;
3
4 interface
5
6 uses
7 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
8 Dialogs, StdCtrls;
9
10 type
11 TForm1 = class(TForm)
12 Button1: TButton;
13 ListBox1: TListBox;
14 procedure Button1Click(Sender: TObject);
15 procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
16 Rect: TRect; State: TOwnerDrawState);
17 private
18 { Private declarations }
19 public
20 { Public declarations }
21 end;
22
23 var
24 Form1: TForm1;
25
26 implementation
27
28 {$R *.dfm}
29
30 procedure TForm1.Button1Click(Sender: TObject);
31 begin
32 ListBox1.AddItem('Test',nil);
33 ListBox1.AddItem('Test II',nil);
34 ListBox1.AddItem('Test III',nil);
35
36 end;
37
38 procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
39 Rect: TRect; State: TOwnerDrawState);
40 var
41 ListColor: TColor;
42 ListBrush: TBrush;
43 begin { TForm1.ListBox1DrawItem }
44
45 // create a brush to paint the item's background
46 ListBrush := TBrush.Create;
47
48 // get a canvas to draw - this is a canvas for the complete listbox!
49 with (Control as TListBox).Canvas do
50 begin
51 // put out lines in alternating colors
52 if (index mod 2)=0 then
53 ListColor := clSilver
54 else
55 ListColor := clBlue;
56
57 ListBrush.Style := bsSolid;
58 ListBrush.Color := ListColor;
59 Windows.FillRect(Handle, Rect, ListBrush.Handle);
60 Brush.Style := bsClear;
61
62 // write out the text
63 TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[index]);
64
65 ListBrush.Free
66 end; { with (Control as TListBox).Canvas }
67
68
69 end;
70
71 end.
|