2010. június 7., hétfő

Check if the mouse is over a tab of a TTabControl


Problem/Question/Abstract:

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:

procedure TForm1.TabControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
  Integer);
var
  XHitTests: THitTests;
begin
  XHitTests := TabControl1.GetHitTestInfoAt(X, Y);
  if htOnItem in XHitTests then
    ShowMessage('OnItem');
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:

procedure TForm1.TabControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
  Integer);
var
  XHitTestInfo: TTCHitTestInfo;
  XIndex: integer;
begin
  XHitTestInfo.pt := POINT(X, Y);
  XIndex := TabCtrl_HitTest(TabControl1.Handle, @XHitTestInfo);
  if XHitTestInfo.flags in [TCHT_ONITEM] then
    ShowMessage('OnItem ' + TabControl1.Tabs[XIndex])
  else if XHitTestInfo.flags in [TCHT_ONITEMICON] then
    ShowMessage('OnIcon ' + TabControl1.Tabs[XIndex])
  else if XHitTestInfo.flags in [TCHT_ONITEMLABEL] then
    ShowMessage('OnLabel ' + TabControl1.Tabs[XIndex]);
end;

Nincsenek megjegyzések:

Megjegyzés küldése