Popup menu for a Delphi Firemonkey form by Scott Hollows

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,439
Credits
574
Popup menu for a Delphi Firemonkey form
Scott Hollows - 08/Jan/2017
[SHOWTOGROUPS=4,20]
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));

[/SHOWTOGROUPS]