随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


相关控件: TMenuBar、TPopupMenu、TMainMenu; 它们都是要包含 TMenuItem; 在设计时添加 TMenuItem 很容易.
其中的 TMainMenu 暂不能应用其他样式; TMenuBar 只有一个值得注意 UseOSMenu 属性.

控件 PopupMenu 属性用于指定右键菜单.

暂时无法直接为窗体指定右键菜单, 因为窗体现在没有 PopupMenu 属性; 我想到的办法是在窗体上覆盖一个 TPanel 或 TRectangle:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel1.Align := TAlignLayout.alClient;
  Panel1.StyleLookup := StyleLookup;
  Panel1.PopupMenu := PopupMenu1;
end;


也可通过 TPopupMenu 的 Popup() 方法:

procedure TForm1.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
  pt: TPointF;
begin
  inherited;
  if Button = TMouseButton.mbRight then
  begin
    pt := PointF(x,y);
    pt := ClientToScreen(pt);
    PopupMenu1.Popup(pt.X, pt.Y);
  end;
end;


Popup() 方法用于控件的例子(如 TRectangle):

procedure TForm1.Rectangle1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
var
  pt: TPointF;
begin
  if Button = TMouseButton.mbRight then
  begin
    pt := PointF(x,y);
    pt := TControl(Sender).LocalToAbsolute(pt);
    pt := ClientToScreen(pt);
    PopupMenu1.Popup(pt.X, pt.Y);
  end;
end;


TPopupMenu 的功能很简单, 更多需要在 TMenuItem 中.

以下测试都需要在空白窗体上先放置 Rectangle1、PopupMenu1.

动态添加菜单项:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item2';
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := '-';
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item3';
  end;
end;


嵌套菜单项:

procedure TForm1.FormCreate(Sender: TObject);
var
  item: TMenuItem;
begin
  Rectangle1.PopupMenu := PopupMenu1;

  item := TMenuItem.Create(Self);
  item.Parent := PopupMenu1;
  item.Text := 'Item1';

    with TMenuItem.Create(Self) do
    begin
      Parent := item;
      Text := 'Item1_1';
    end;
    with TMenuItem.Create(Self) do
    begin
      Parent := item;
      Text := 'Itme1_2';
    end;

  with TMenuItem.Create(Self) do
  begin
    Parent := PopupMenu1;
    Text := 'Item2';
  end;
end;


指定快捷键:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
    ShortCut := scCtrl or Byte('A'); //Ctrl + A
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item2';
    ShortCut := scShift or scCtrl or scAlt or Ord('A'); //Shift + Ctrl + Alt + A
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := '-';
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item3';
    ShortCut := 112; //F1
  end;
end;


复选菜单项:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
    AutoCheck := True;
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item2';
    AutoCheck := True;
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := '-';
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item3';
    AutoCheck := True;
  end;
end;


单选(分组)菜单项:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
    AutoCheck := True;
    RadioItem := True;
    GroupIndex := 1;
    IsChecked := True;
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item2';
    AutoCheck := True;
    RadioItem := True;
    GroupIndex := 1;
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := '-';
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item3';
    AutoCheck := True;
    RadioItem := True;
    GroupIndex := 2;
  end;
  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item4';
    AutoCheck := True;
    RadioItem := True;
    GroupIndex := 2;
  end;
end;


菜单文本格式:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
    Font.Style := [TFontStyle.fsBold, TFontStyle.fsItalic];
  end;
end;


图标:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(Self) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
    Bitmap.LoadFromFile('c:\temp\test.png');
  end;
end;


指定事件:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Menus, FMX.Objects;

type
  TForm1 = class(TForm)
    Rectangle1: TRectangle;
    PopupMenu1: TPopupMenu;
    procedure FormCreate(Sender: TObject);
    procedure ItemOnClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Rectangle1.PopupMenu := PopupMenu1;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item1';
    OnClick := ItemOnClick;
  end;

  with TMenuItem.Create(PopupMenu1) do
  begin
    Parent := PopupMenu1;
    Text := 'Item2';
    OnClick := ItemOnClick;
  end;
end;

procedure TForm1.ItemOnClick(Sender: TObject);
begin
  ShowMessage(TTextControl(Sender).Text);
end;

end.

posted on   万一  阅读(8770)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧


点击右上角即可分享
微信分享提示