关于Firemonkey Delphi 10.4中TPopupMenu定位的问题

huangapple go评论65阅读模式
英文:

A problem with TPopupMenu positioning in Firemonkey Delphi 10.4

问题

以下是您提供的问题的翻译部分:

我之前有一个关于这个问题的问题格式很差,所以我决定删除之前的问题,重新提出一个更好(和更小)的示例。

我在**Firemonkey(Delphi 10.4)**中遇到了一个非常奇怪的TPopupMenu组件的行为。为了模拟这个问题,我创建了一个FORM,并放置了以下内容:

  1. 在表单上放置了一个TPopupMenu组件,并分配了一些项目
  2. 在其中放置了一个TLayout(Firemonkey中的FORM没有属性PopupMenu)
  3. 由于同样的原因,在其中放置了一个TPanel,其中属性为PopupMenu1
  4. 在表单中央放置了一个TButton

奇怪的是,只有右键按下几次,弹出菜单才会出现,当它出现时,它离鼠标光标很远。
在“弹出”弹出菜单之前是否需要执行任何操作?
我在Embarcadero文档中搜索但没有成功。
我已经准备了一些代码。

这是该单元的代码:

unit MainUnitTestPopup;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.Menus;

type
  TForm2 = class(TForm)
    Layout1: TLayout;
    Button1: TButton;
    PopupMenu1: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.Button1Click(Sender: TObject);
begin
   Application.Terminate;
end;

end.

这是FORM的文本代码:

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object Layout1: TLayout
    PopupMenu = PopupMenu1
    Size.Width = 177.000000000000000000
    Size.Height = 480.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
    object Button1: TButton
      Position.X = 216.000000000000000000
      Position.Y = 168.000000000000000000
      Size.Width = 185.000000000000000000
      Size.Height = 65.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
      Text = 'CLICK ME!'
      OnClick = Button1Click
    end
  end
  object Panel1: TPanel
    PopupMenu = PopupMenu1
    Position.X = 176.000000000000000000
    Position.Y = 232.000000000000000000
    Size.Width = 465.000000000000000000
    Size.Height = 257.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 2
  end
  object PopupMenu1: TPopupMenu
    Left = 520
    Top = 56
    object MenuItem1: TMenuItem
      Text = 'MenuItem1'
      object MenuItem2: TMenuItem
        Locked = True
        Text = 'MenuItem2'
      end
    end
  end
end

这是项目的代码:

program TestPopupProject;

uses
  System.StartUpCopy,
  FMX.Forms,
  MainUnitTestPopup in 'MainUnitTestPopup.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

希望这次我已经能够创建一个格式正确的问题。
谢谢您的帮助(请原谅我在提问方面的不足)。

英文:

My previous question on this problem was poorly formatted so I decided to delete the previous question and repropose it with a better (and smaller) example.

I'm experiencing a very strange behavior of the TPopupMenu component in Firemonkey (Delphi 10.4).
To simulate the problem, I have created a FORM and I have placed:

  1. a TPopupMenu component on the form and I have assigned a couple of items
  2. a TLayout on it (the FORM in Firemonkey does not have a property PopupMenu)
  3. a TPanel, for the same reason, with the PopupMenu1 as property
  4. a Tbutton in the center of the Form
    Well, it happens that only a few times of the Right-Button press the pop up menu appears, and when it does, it is far away from the mouse cursor.
    Is there any assignment which must be done before to "popup" the popup?
    What am I doing wrong? I searched on the Embarcadero documentation without success.
    I have prepared a bit of code.

This is the code of the unit:

unit MainUnitTestPopup;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.Menus;

type
  TForm2 = class(TForm)
    Layout1: TLayout;
    Button1: TButton;
    PopupMenu1: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.Button1Click(Sender: TObject);
begin
   Application.Terminate;
end;

end.

and this is the textcode of the FORM

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object Layout1: TLayout
    PopupMenu = PopupMenu1
    Size.Width = 177.000000000000000000
    Size.Height = 480.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
    object Button1: TButton
      Position.X = 216.000000000000000000
      Position.Y = 168.000000000000000000
      Size.Width = 185.000000000000000000
      Size.Height = 65.000000000000000000
      Size.PlatformDefault = False
      TabOrder = 0
      Text = 'CLICK ME!'
      OnClick = Button1Click
    end
  end
  object Panel1: TPanel
    PopupMenu = PopupMenu1
    Position.X = 176.000000000000000000
    Position.Y = 232.000000000000000000
    Size.Width = 465.000000000000000000
    Size.Height = 257.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 2
  end
  object PopupMenu1: TPopupMenu
    Left = 520
    Top = 56
    object MenuItem1: TMenuItem
      Text = 'MenuItem1'
      object MenuItem2: TMenuItem
        Locked = True
        Text = 'MenuItem2'
      end
    end
  end
end

and this is the code of the project:

program TestPopupProject;

uses
  System.StartUpCopy,
  FMX.Forms,
  MainUnitTestPopup in 'MainUnitTestPopup.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

I hope this time I've been able to create a correctly formatted question.
Thank you for your help (and pls excuse my lack of expertise in asking).

答案1

得分: 1

代码部分不需要翻译。以下是翻译好的部分:

"When I try it (also with Delphi 10.4), the popup menu opens at the cursor position as expected."

  • 当我尝试它(也在Delphi 10.4中),弹出菜单如预期般在光标位置打开。

"However, it only appears when right-clicking on the panel because the TLayout ignores the mouse click."

  • 但是,它只在右键单击面板时出现,因为TLayout忽略鼠标单击。

"To change this, check the HitTest box in the object inspector. Then you can pop up the menu on the TLayout also."

  • 要更改这一点,请在对象检视器中选中HitTest框。然后您也可以在TLayout上弹出菜单。

"In addition, if you want the pop-up menu to work anywhere on the form, you should have the TLayout fill the form. This is best achieved by setting its Align property to Client or Contents."

  • 另外,如果您希望弹出菜单在窗体的任何地方都起作用,您应该让TLayout填充整个窗体。最好的方式是将其Align属性设置为ClientContents

"Although even then it still won't work when right-clicking on another control, such as the button unless you also set the control's PopupMenu property to the popup menu."

  • 即使如此,除非您还将控件的PopupMenu属性设置为弹出菜单,否则当右键单击其他控件(例如按钮)时,它仍然不起作用。
英文:

When I try it (also with Delphi 10.4), the popup menu opens at the cursor position as expected.

However, it only appears when right-clicking on the panel, because, the TLayout ignores the mouse click. To change this, check the HitTest box in the object inspector. Then you can pop up the menu on the TLayout also.

In addition, if you want the pop-up menu to work anywhere on the form, you should have the TLayout fill the form. This is best achieved by setting it's Align property to Client or Contents. Although even then it still won't work when right-clicking on another control, such as the button (unless you also set the control's PopupMenu property to the popup menu).

答案2

得分: 0

I'm tired to fight against this problem, that maybe related to an unnumerable series of possibilities. The fact that my installation of Delphi 10.4.2 works differently by those of yours makes me think that there may be something corrupted or broken in my installation. I will repeat the installation once I've time to do that.
所以我自己创建了一个弹出菜单类,它在我需要的所有情况下都工作正常,而且代码不到200行,包括弹出元素的声明。
让我对Philip J. RaymentTom Brunberg表示非常感谢,他们提供了不胜感激的支持。

英文:

I'm tired to fight against this problem, that maybe related to an unnumerable series of possibilities. The fact that my installation of Delphi 10.4.2 works differently by those of yours makes me think that there may be something corrupted or broken in my installation. I will repeat the installation once I've time to do that.
So I created a popup menu class by myself, which works fine in all the circumstances I needed, and it is less than 200 lines of code, including the declarations of the popup elements.
Let me say a BIG THANK YOU to both Philip J. Rayment and Tom Brunberg for your more than kind support.

huangapple
  • 本文由 发表于 2023年3月3日 19:41:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定