Author: Tomas Rutkauskas
How to draw a bitmap between the checkbox and the label in a TCheckListBox
Answer:
This should do the trick. It is also possible to place a bitmap in the middle, i.e.
The checkbox, then some text, the graphic, then more text, on the same line. The
Checklistbox style must be set to lbOwnerDrawVariable for this to work.
1 procedure TForm1.Button2Click(Sender: TObject);
2 begin
3 {Bit1 is called from a resource file}
4 CheckListBox1.Items.AddObject('Test this bitmap', Bit1);
5 end;
6
7 procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
8 Rect: TRect; State: TOwnerDrawState);
9 var
10 Bitmap: TBitmap;
11 Offset: Integer;
12 begin
13 Offset := 12;
14 with (Control as TCheckListBox).Canvas do
15 begin
16 FillRect(Rect);
17 Bitmap := TBitmap(CheckListBox1.Items.Objects[Index]);
18 if Bitmap <> nil then
19 begin
20 BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width, Bitmap.Height),
21 Bitmap, Bounds(0, 0, Bitmap.Width, Bitmap.Height), clRed);
22 Offset := Bitmap.width + 8;
23 end;
24 TextOut(Rect.Left + Offset, Rect.Top, CheckListbox1.Items[Index])
25 end;
26 end;
27
28 procedure TForm1.CheckListBox1KeyUp(Sender: TObject; var Key: Word; Shift:
29 TShiftState);
30 begin
31 height := 16;
32 end;
|