Author: Tomas Rutkauskas
I try to determine if the mouse is just over a tab of a TTabControl using
GetHitTestInfoAt(). This returns htOnItem if the mouse is over the tab and if it's
not. How do I have to approach this issue?
Answer:
Probably, the problem is in the routine which calls GetHitTestInfoAt, since I've
tried to check this method and all seemed to work fine. I was calling it in the
form's and tabcontrols's OnMouseMove events:
1 2 procedure TForm1.TabControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
3 Integer);
4 var5 XHitTests: THitTests;
6 begin7 XHitTests := TabControl1.GetHitTestInfoAt(X, Y);
8 if htOnItem in XHitTests then9 ShowMessage('OnItem');
10 end;
Also, you can try to call Win API macro TabCtrl_HitTest directly, in order to
determine which tab, if any, is over the cursor. Here's an example:
11 12 procedure TForm1.TabControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
13 Integer);
14 var15 XHitTestInfo: TTCHitTestInfo;
16 XIndex: integer;
17 begin18 XHitTestInfo.pt := POINT(X, Y);
19 XIndex := TabCtrl_HitTest(TabControl1.Handle, @XHitTestInfo);
20 if XHitTestInfo.flags in [TCHT_ONITEM] then21 ShowMessage('OnItem ' + TabControl1.Tabs[XIndex])
22 elseif XHitTestInfo.flags in [TCHT_ONITEMICON] then23 ShowMessage('OnIcon ' + TabControl1.Tabs[XIndex])
24 elseif XHitTestInfo.flags in [TCHT_ONITEMLABEL] then25 ShowMessage('OnLabel ' + TabControl1.Tabs[XIndex]);
26 end;