Author: Tomas Rutkauskas
Is there a possibility to get tooltips in a common TListView component under Delphi
4.0? I want to display details if the user moves the mouse over an item and wait a
little (same function like the component names in Delphi, if you move your mouse
over a component).
Answer:
There is an event handler in Delphi 5, which makes it possible for you to get
tooltips for each item of a ListView easily: TListView.OnInfoTip. In Delphi 3 and
4, you have to write your own hint event handler, which you assign to the method
OnShowHint of TApplication:
1 unit Test_u1;
2 { ... }3 4 type5 TForm1 = class(TForm)
6 ListView1: TListView;
7 { ... }8 private9 procedure DisplayHint(var HintStr: string; var CanShow: Boolean; var HintInfo:
10 THintInfo);
11 end;
12 13 { ... }14 15 implementation16 17 {$R *.DFM}18 19 procedure TForm1.FormCreate(Sender: TObject);
20 var21 NewItem: TListItem;
22 begin23 Application.OnShowHint := DisplayHint;
24 { ... }25 end;
26 27 procedure TForm1.DisplayHint;
28 var29 Item: TListItem;
30 Rect: TRect;
31 begin32 CanShow := true;
33 {Trace the item of ListView1, which is found on the mouse position X, Y.34 If the mouse isn't dragged over a item, result will be nil.}35 Item := ListView1.GetItemAt(HintInfo.CursorPos.X, HintInfo.CursorPos.Y);
36 if Item <> nilthen37 begin38 Rect := Item.DisplayRect(drBounds); {in coordinates of ListView1!}39 HintInfo.HintStr := 'Mouse is over Item ' + Item.Caption;
40 end41 else42 begin43 Rect := ActiveControl.ClientRect;
44 HintInfo.HintStr := GetShortHint(TControl(ActiveControl).Hint);
45 end;
46 { Converting into coordinates of screen. }47 Rect.TopLeft := ActiveControl.ClientToScreen(Rect.TopLeft);
48 Rect.BottomRight := ActiveControl.ClientToScreen(Rect.BottomRight);
49 with HintInfo do50 begin51 HintPos.Y := Rect.Top + GetSystemMetrics(SM_CYCURSOR);
52 HintPos.X := Rect.Left + GetSystemMetrics(SM_CXCURSOR);
53 HintMaxWidth := TControl(ActiveControl).ClientWidth;
54 HintColor := clInfoBk;
55 ReshowTimeout := 10;
56 HideTimeout := 100;
57 end;
58 end;
59 60 end.
61 62 {BTW: The type THintInfo is used to define the appearance and the function of the 63 HintWindow:}64 65 type66 THintWindowClass = classof THintWindow;
67 68 THintInfo = record69 HintControl: TControl;
70 HintWindowClass: THintWindowClass;
71 HintPos: TPoint;
72 HintMaxWidth: Integer;
73 HintColor: TColor;
74 CursorRect: TRect;
75 CursorPos: TPoint;
76 ReshowTimeout: Integer;
77 HideTimeout: Integer;
78 HintStr: string;
79 HintData: Pointer;
80 end;