Search code examples
delphiclickcontextmenutpagecontrol

Adding a context menu to the tab of a TPageControl


I wish to add a context menu to the (just the) tab of a TPageControl as distinct from the tab area (e.g like Delphi does to offer file/page options). I know I can do this with TRzPageControl but how might it be possible with TPageControl please?


Solution

  • If you don't want to create a component, you can always use the OnContextPopup of your PageControl and depending on the Mouse position switch its PopupMenu.

    Assume you have created 2 PopuMenus pmTabs ans pmPages, the following code will display the 1st when hitting the tabs area and the 2nd otherwise:

    procedure TForm2.PageControl1ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    begin
      with Sender as TPageControl do begin
        if [htOnItem] * GetHitTestInfoAt(MousePos.X, MousePos.Y) <> [] then
          PopupMenu := pmTabs
        else
          PopupMenu := pmPages;
      end;
    end;