Author: Tomas Rutkauskas
How do I get CheckBoxes when using a TListView with OwnerData set to true and using
an OnData event method?
Answer:
1 { ... }
2 const
3 W_64: Word = 64; {Width of thumbnail in ICON view mode}
4 H_64: Word = 64; {Height of thumbnail size}
5 CheckWidth: Word = 14; {Width of check mark box}
6 CheckHeight: Word = 14; {Height of checkmark}
7 CheckBiasTop: Word = 2; {This aligns the checkbox to be in centered}
8 CheckBiasLeft: Word = 3; {In the row of the list item display}
9
10 procedure DrawCheckMark(const ListViewX: TListView; Item: TListItem; Checked:
11 Boolean);
12 var
13 TP1, TP2: TPoint;
14 XBias, YBias: Integer;
15 OldColor: TColor;
16 BiasTop, BiasLeft: Integer;
17 Rect1: TRect;
18 begin
19 GetCheckBias(XBias, YBias, BiasTop, BiasLeft, ListViewX);
20 OldColor := ListViewX.Canvas.Pen.Color;
21 TP1 := Item.GetPosition;
22 if Checked then
23 ListViewX.Canvas.Brush.Color := clBlack;
24 Rect1.Left := Item.Left - CheckWidth - BiasLeft + 1 + XBias;
25 Rect1.Top := Tp1.Y + BiasTop + 1 + YBias;
26 Rect1.Right := Item.Left - BiasLeft - 1 + XBias;
27 Rect1.Bottom := Tp1.Y + BiasTop + CheckHeight - 1 + YBias;
28 ListViewX.Canvas.FillRect(Rect1);
29 if Checked then
30 ListViewX.Canvas.Brush.Color := clBlue
31 else
32 ListViewX.Canvas.Brush.Color := clBlack;
33 ListViewX.Canvas.FrameRect(Rect1);
34 ListViewX.Canvas.FrameRect(Rect(Rect1.Left - 1, Rect1.Top - 1,
35 Rect1.Right + 1, Rect1.Bottom + 1));
36 if Checked then
37 begin
38 ListViewX.Canvas.Pen.Color := clLime;
39 TP2.X := Item.Left - BiasLeft - 2 + XBias;
40 TP2.Y := Tp1.Y + BiasTop + 2 + YBias;
41 ListViewX.Canvas.PenPos := TP2;
42 ListViewX.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth div 2) +
43 XBias, Tp1.Y + BiasTop + (CheckHeight - 2) + YBias);
44 ListViewX.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth - 2) + XBias,
45 Tp1.Y + BiasTop + (CheckHeight div 2) + YBias);
46 TP2.X := Item.Left - BiasLeft - 2 - 1 + XBias;
47 TP2.Y := Tp1.Y + BiasTop + 2 + YBias;
48 ListViewX.Canvas.PenPos := TP2;
49 ListViewX.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth div 2) - 1 + XBias,
50 Tp1.Y + BiasTop + (CheckHeight - 2) + YBias);
51 ListViewX.Canvas.LineTo(Item.Left - BiasLeft - (CheckWidth - 2) - 1 + XBias,
52 Tp1.Y + BiasTop + (CheckHeight div 2) + YBias);
53 end;
54 ListViewX.Canvas.Brush.Color := ListViewX.Color;
55 ListViewX.Canvas.Pen.Color := OldColor;
56 end;
57
58 procedure GetCheckBias(var XBias, YBias, BiasTop, BiasLeft: Integer;
59 const ListView: TListView);
60 begin
61 XBias := 0;
62 YBias := 0;
63 if ListView.ViewStyle = vsICON then
64 begin
65 YBias := H_64 - CheckHeight;
66 XBias := 0;
67 end;
68 BiasTop := CheckBiasTop;
69 BiasLeft := CheckBiasLeft;
70 if ListView.ViewStyle <> vsReport then
71 begin
72 BiasTop := 0;
73 BiasLeft := 0;
74 end;
75 end;
76
77 //In the OnCustomDrawItem event you would do something like this:
78
79 DrawCheckMark(FileListMenu.ListView1, Item, Item.Checked);
|