Popup menu for a Delphi Firemonkey form

Unlike VCL forms, Delphi FireMonkey forms do not have a PopupMenu property.  So how can you show a popup menu when the user clicks the Right Mouse Button ?

You have to code it yourself and its pretty easy to do.  The tricky bit is converting the X/Y  position from the forms local co-ordinate system to global co-ordinates.  Ill show you to do that.

popup_menu_1

Instructions

  1. Create a popup menu on the form
    Add menu items to it in the usual way.
    I presume you know how to do.

popup_menu_2

2. Create an OnMouseDown event for the form with this code

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
   vPoint : TPointF;
begin
// show popup menu when right mouse button
   if Button = TMouseButton.mbRight then
      begin
                       -- convert local XY to global XY
      vPoint := Self.ClientToScreen(PointF (X,Y));
      PopupMenu1.Popup(vPoint.X, vPoint.Y);
      end;
end;

3. Run the project (F9 Key)

4. Right click on the form.
The popup menu should appear at the mouse location

Convert local co-ordinates to global co-ordinates

Popup menus can be displayed anywhere on the screen, even outside of the form’s display area.  As a result, the X/Y position for the popup menu is defined using global coordinates instead of the Forms local co-ordinates.

The above code converts the X and Y arguments from local co-ordinate to global co-ordinates like this:

vPoint := Self.ClientToScreen(PointF (X,Y));

+1 this post

If you like this post, please +1 vote here to get it listed on Delphi Feeds and also here on BeginEnd.  Thank You

About Me

scott_hollows_201611

  • Oracle & Delphi software developer based in Perth, Western Australia
  • Australian Delphi User Group – WA Chief Cat Herder
  • Australian Delphi User Group – President
blog email linkedinlogo

Author: Scott Hollows

Enterprise software developer based in Perth, Western Australia. Focused on Oracle, Delphi, Data Warehouse design and ETL, Data Architect, Business Intelligence, Oracle performance tuning. President of the Australian Delphi User Group (ADUG) LinkedIn www.linkedin.com/in/scotthollows

1 thought on “Popup menu for a Delphi Firemonkey form”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s