• Delphi动态添加菜单


    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, Menus, StdCtrls, ComCtrls;

    type
    TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    Button1: TButton;
    N0000000001: TMenuItem;
    RichEdit1: TRichEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure N0000000001Click(Sender: TObject);
    private
    procedure MyClick(Sender: TObject);
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    MainForm: TMainForm;
    subItemName:String;

    implementation

    {$R *.dfm}

    procedure TMainForm.FormCreate(Sender: TObject);
    var addSubItem:TMenuItem;
    i:Integer;
    N:Integer;
    SubMenuItemSource:String;
    begin
    N:=5;
    SubMenuItemSource:='动态-';
    for i:=0 to N do //N等于要添加动态菜单数目,需事先定义为数值型变量,并赋值
    begin
    addSubItem:= TMenuItem.Create(Self);
    addSubItem.Name := 'A'+IntToStr(i);
    addSubItem.Caption := SubMenuItemSource+IntToStr(i);// 步骤1的SubMenuItemSource应先赋值
    //FileOpenItem.Add(addSubItem);//在名称为FileOpenItem的菜单项下添加子菜单
    addSubItem.OnClick:=self.MyClick;//步骤2的MyClick(),自定义菜单要响应的事件
    self.MainMenu1.Items.Add(addSubItem);
    end
    end;

    procedure TMainForm.MyClick(Sender: TObject); //动态菜单OnClick事件响应
    begin
    ShowMessage(TMenuItem(Sender).Caption); //显示选中的动态菜单标题,
    end;

    procedure TMainForm.Button1Click(Sender: TObject);
    var addSubItem:TMenuItem;
    i:Integer;
    N:Integer;
    SubMenuItemSource:String;
    begin
    self.MainMenu1.Items[0].OnClick:=nil; //变为无事件
    N:=5;
    SubMenuItemSource:='KKKKK-';
    subItemName:=subItemName+'T';
    for i:=0 to N do //N等于要添加动态菜单数目,需事先定义为数值型变量,并赋值
    begin
    addSubItem:= TMenuItem.Create(Self);
    addSubItem.Name := subItemName+IntToStr(i);
    addSubItem.Caption := SubMenuItemSource+IntToStr(i);// 步骤1的SubMenuItemSource应先赋值
    //FileOpenItem.Add(addSubItem);//在名称为FileOpenItem的菜单项下添加子菜单
    addSubItem.OnClick:=self.MyClick;//步骤2的MyClick(),自定义菜单要响应的事件
    self.MainMenu1.Items[0].Add(addSubItem);
    end
    end;


    procedure TMainForm.N0000000001Click(Sender: TObject);
    begin
    showmessage('aaa');
    end;

    end.

  • 相关阅读:
    记一个微信公众号个性化菜单的坑
    初学c# -- 学习笔记(六) winfrom组件圆角
    Swift中添加标记(MARK)和警告(TODO FIXME)
    iOS Xib布局某些控件显示或隐藏<约束的修改>
    [!] Unable to satisfy the following requirements:
    React Native使用NetInfo对当前系统网络的判断
    iOS自定义下拉列表
    [!] The version of CocoaPods used to generate the lockfile (1.4.0.beta.1) is higher than the version of the current executable (1.3.0.beta.1). Incompatibility issues may arise.
    iOS实现TextField光标居中
    iOS优秀第三方框架
  • 原文地址:https://www.cnblogs.com/jijm123/p/7456318.html
Copyright © 2020-2023  润新知