Author: Tomas Rutkauskas
How to draw multiple columns in a TComboBox
Answer:
You can go with a custom drawn combo box:
1 unit Unit1;
2
3 interface
4
5 uses
6 SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
7 Forms, Dialogs, StdCtrls, ExtCtrls;
8
9 type
10
11 TDataRec = class(TObject)
12 private
13 Str1: string;
14 Str2: string;
15 end;
16
17 TForm1 = class(TForm)
18 DeleteListBox1: TListBox;
19 Header1: THeader;
20 Image1: TImage;
21 ComboBox1: TComboBox;
22 procedure FormCreate(Sender: TObject);
23 procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
24 State: TOwnerDrawState);
25 private
26 { Private declarations }
27 public
28 { Public declarations }
29 end;
30
31 var
32 Form1: TForm1;
33
34 implementation
35
36 {$R *.DFM}
37
38 procedure TForm1.FormCreate(Sender: TObject);
39 var
40 i: integer;
41 DataRec: TDataRec;
42 begin
43 for i := 1 to 10 do
44 begin
45 DataRec := TDataRec.Create;
46 with DataRec do
47 begin
48 Str1 := 'String1 ' + IntToStr(i);
49 Str2 := 'String2 ' + IntToStr(i);
50 end;
51 ComboBox1.Items.AddObject('', DataRec);
52 end;
53 end;
54
55 procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect:
56 TRect;
57 State: TOwnerDrawState);
58 var
59 S1, S2: string;
60 TempRect: TRect;
61 begin
62 S1 := TDataRec(ComboBox1.Items.Objects[Index]).Str1;
63 S2 := TDataRec(ComboBox1.Items.Objects[Index]).Str2;
64 ComboBox1.Canvas.FillRect(Rect);
65 TempRect := Rect;
66 TempRect.Right := Header1.SectionWidth[0];
67 ComboBox1.Canvas.StretchDraw(TempRect, Image1.Picture.Graphic);
68 Rect.Left := Rect.Left + 50;
69 DrawText(ComboBox1.Canvas.Handle, PChar(S1), Length(S1), Rect, dt_Left or
70 dt_VCenter);
71 Rect.Left := Rect.Left + 100;
72 DrawText(ComboBox1.Canvas.Handle, PChar(S1), Length(S1), Rect, dt_Left or
73 dt_VCenter);
74 end;
75
76 end.
|