Mirror

Create a popup menu for a tab of a TPageControl (Views: 100)


Problem/Question/Abstract:

How to create a popup menu for a tab of a TPageControl

Answer:

{ ... }
uses
  commctrl;

procedure TabMenuPopup(APageControl: TPageControl; X, Y: Integer; );
var
  hi: TTCHitTestInfo;
  TabIndex: Integer;
  p: TPoint;
begin
  hi.pt.x := X;
  hi.pt.y := Y;
  hi.flags := 0;
  TabIndex := APageControl.Perform(TCM_HITTEST, 0, longint(@hi));
  p.x := APageControl.Left + X;
  p.y := APageControl.Top + y;
  p := ClientToScreen(p);
  {Allows use of different menus for each tab...}
  case TabIndex of
    0: {on the first tab...}
      PopupMenu1.Popup(P.x, P.Y);
    1: {on the second tab...}
      PopupMenu2.Popup(P.x, P.Y);
  end;
end;
end;

procedure TForm1.PageControl1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
  begin
    TabMenuPopup(PageControl1, X, Y);
  end;
end;

<< Back to main page