Detecting Which Tab the Mouse Cursor is Over

By using the TabCtrl_HitTest() macro, you can determine which Tab the mouse cursor is over.  In this way, you can prevent Tabs from being selected, or highlight the Tab when the mouse is over a certain item / area.  The example below uses the OnChanging() event handler to prevent the first Tab in a PageControl from being selected.  The same method can be used for a TabControl.
 

KEYWORDS: TabCtrl_HitTest, TC_HITTESTINFO
 



 

//---------------------------------------------------------------------------
void __fastcall TForm1::PageControl1Changing(TObject *Sender, bool &AllowChange)
{
    TC_HITTESTINFO hti;
    POINT P;
    int tab_index;

    GetCursorPos(&P);
    P = PageControl1->ScreenToClient(P);

    hti.pt.x = P.x;
    hti.pt.y = P.y;

    tab_index = PageControl1->Perform(TCM_HITTEST, 0, (LPARAM)&hti);

    // prevent first tab from being selected for example...
    if (tab_index == 0) AllowChange = false;
}