Author: Tomas Rutkauskas
How to display hints always under the mouse cursor
Answer:
This code snippet shows how to make your popup hint windows behave more like normal
windows apps. Instead of always being square under the control they belong to, they
are based on where the mouse is. This uses the GetIconInfo API, which is only
available for Win32.
Add the following to your main form's OnCreate event handler:
1 procedure TMainForm.FormCreate(Sender: TObject);
2 begin3 Application.OnShowHint := GetHintInfo;
4 end;
Add the following declaration to your main form's protected declartion:
procedure GetHintInfo(var HintStr: string; var CanShow: boolean; var HintInfo:
THintInfo);
And, finally, add this procedure to your main form:
5 6 procedure TMainForm.GetHintInfo(var HintStr: string; var CanShow: boolean; var7 HintInfo: THintInfo);
8 var9 II: TIconInfo;
10 Bmp: Windows.TBitmap;
11 begin12 with HintInfo do13 begin14 {Make sure we have a control that fired the hint}15 if HintControl = nilthen16 exit;
17 {Convert the cursor's coordinates from relative to hint to relative to screen}18 HintPos := HintControl.ClientToScreen(CursorPos);
19 {Get some information about the cursor that is used for the hint control}20 GetIconInfo(Screen.Cursors[HintControl.Cursor], II);
21 {Get some information about the bitmap representing the cursor}22 GetObject(II.hbmMask, SizeOf(Windows.TBitmap), @Bmp);
23 {If the info did not include a color bitmap then the mask bitmap is really two 24 bitmaps, an AND & XOR mask. Increment our Y position by the bitmap's height}25 if II.hbmColor = 0 then26 inc(HintPos.Y, Bmp.bmHeight div 2)
27 else28 inc(HintPos.Y, Bmp.bmHeight);
29 {Subtract out the Y hotspot position}30 dec(HintPos.Y, II.yHotSpot);
31 {We are responsible for cleaning up the bitmap handles returned by GetIconInfo}32 DeleteObject(II.hbmMask);
33 DeleteObject(II.hbmColor);
34 end;
35 end;