1
votes

In a Delphi XE7 Firemonkey project i want to block the default Popup-Menu that show up when the user presses the right mouse button on a control.

In VCL you could easily set Handled := true in ContextPopup (link)

Unfortunately this event doesn't exist in FMX.

Is there any way to archieve this in Firemonkey?

1

1 Answers

3
votes

You can prevent the default popoup menu by adding an empty (no menu items) TPopupMenu to the form and assigning it to the TEdit.PopupMenu.

Internally TStyledEdit checks (in TStyledEdit.ShowContextMenu() ) if the PopupMenu property is not nil, and if so, shows the user defined popup menu, otherwise it shows the default popup menu. This has an effect however, that it interrupts editing if the TEdit is right clicked, because the popup menu still enters its own message loop. Thus, the user has to press Esc or left click to continue editing.

An enhancement would be to define a new TPopupMenu class with which you can control whether the popup is enabled or not:

type
  TPopupMenu = class(FMX.Menus.TPopupMenu)
  private
    FEnabled: boolean;
  public
    procedure Popup(X, Y: Single); override;
    property Enabled: boolean read FEnabled Write FEnabled;
  end;

  TForm5 = class(TForm)
    Edit1: TEdit;
    PopupMenu1: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


implementation

{$R *.fmx}

procedure TForm5.FormCreate(Sender: TObject);
begin
  PopupMenu1.Enabled := true;
end;

{ TPopupMenu }

procedure TPopupMenu.Popup(X, Y: Single);
begin
  if FEnabled then
    inherited;
end;

that intercepts the call to Popup() effectively preventing the popup if not enabled.