Author: Tomas Rutkauskas
I would like to have my hints read the same as the listbox item that the mouse is
pointing to. How can I do that?
Answer:
Solve 1:
You could use the listboxes OnMOuseMove event together with the ItemAtPos method.
1 2 procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
3 Integer);
4 var5 item: Integer;
6 begin7 with Sender as TListbox do8 begin9 item := itemAtpos(Point(x, y), true);
10 if item >= 0 then11 begin12 if hint <> items[item] then13 begin14 hint := items[item];
15 application.cancelhint;
16 end;
17 end;
18 end;
19 end;
Solve 2:
You can use the OnMouseMove event and trap which item is under the cursor.
20 21 procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
22 Integer);
23 var24 APoint: TPoint;
25 Index: Integer;
26 begin27 APoint.X := X;
28 APoint.Y := Y;
29 Index := ListBox1.ItemAtPos(APoint, True);
30 if Index >= 0 then31 begin32 ListBox1.Hint := ListBox1.Items.Strings[Index];
33 end;
34 end;
If you want to Hint to change when the mouse moves after the hint is originally
shown, you might want to do something like this:
Set the ShowHint property of the list box to False.
35 36 procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
37 Integer);
38 var39 APoint: TPoint;
40 Index: Integer;
41 HW: THintWindow;
42 Rec: TRect;
43 sHint: string;
44 begin45 APoint.X := X;
46 APoint.Y := Y;
47 Index := ListBox1.ItemAtPos(APoint, True);
48 if Index >= 0 then49 begin50 HW := THintWindow.Create(nil);
51 try52 GetCursorPos(APoint);
53 sHint := ListBox1.Items.Strings[Index];
54 Rec.Top := APoint.Y + 20;
55 Rec.Left := APoint.X;
56 Rec.Right := Rec.Left + HW.Canvas.TextWidth(sHint) + 6;
57 Rec.Bottom := Rec.Top + HW.Canvas.TextHeight(sHint) + 4;
58 HW.ActivateHint(Rec, sHint);
59 HW.Refresh;
60 Sleep(1000);
61 HW.ReleaseHandle;
62 finally63 HW.Free;
64 end;
65 end;
66 end;