How to convert Delphi TPageControl tab index to page index?

I use TPageControl where some pages are not visible.

This disrupts the normal 1: 1 display of the TabIndex and ActivePageIndex properties.

In most cases, I can use ActivePageIndex (or ActivePage itself) to get the current page, but I need a dynamic tooltip, which requires me to determine which page is associated with a particular tab index.

If I call pageControl.IndexOfTabAt (X, Y), I get the tab index back, but I cannot directly use it as an index in the Pages [] array, since some tabs of the pages are not displayed.

I could iterate through the pages, ignoring the visible ones, but it looks like there should be something in VCL that does this for me already ...?

+4
source share
2 answers

I look in the source for TPageControl (ComCtrls.pas), there is a private method:

function TPageControl.PageIndexFromTabIndex(TabIndex: Integer): Integer; 

does what you want. But you cannot call it (D2007), therefore (unfortunately) you need to copy the code.

+2
source

Here's an old article about dragging and dropping pages. It has some logic to get the page index from position (X, Y), maybe you can use it. Something like this (untested):

 function TMyPageControl.GetPageIndexAtPos(X, Y: Integer) : Integer; const TCM_GETITEMRECT = $130A; var TabRect: TRect; j: Integer; begin for j := 0 to PageCount - 1 do begin Perform(TCM_GETITEMRECT, j, LParam(@TabRect)) ; if PtInRect(TabRect, Point(X, Y)) then begin Result := j; exit; end; end; Result := -1; end; 
-one
source

Source: https://habr.com/ru/post/1299421/


All Articles