DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: jeff1314
今日帖子: 9
在线用户: 3
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2020/11/26 17:32:20
标题:
MainMenu控件是不是总是存在一个index为0的MenuItem ? 浏览:736
加入我的收藏
楼主: 我在动态生成菜单的时候发现,
似乎存在一个Index为0的MenuItem,Name和Caption属性均为空,并且不会在界上显示。
用MainMenu.Clear方法,不会清除它。
如果通过Commponets属性及方法清除了它,MainMenu就挂掉了。
----------------------------------------------
z@S7
作者:
男 iamdream (银河恒久远,梦想无止境!) ★☆☆☆☆ -
大贡献会员
2020/11/27 9:40:56
1楼: 可以肯定是你代码的问题。
----------------------------------------------
-广袤璀璨的银河,永无止境的梦想(梦无止境游银河) 博客挂了……
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2020/11/27 10:08:52
2楼: implementation

{$R *.dfm}

uses
  Vcl.Menus;

var
  lMyMainMenu: TMainMenu = nil;

procedure prcCreateMenuItems;
var
  lMyMenuItem: TMenuItem;
begin
  if not(lMyMainMenu = nil) then
  begin
    lMyMenuItem := TMenuItem.Create(lMyMainMenu);
    //
    try
      lMyMenuItem.Name    := Format('MyMenuItem%d', [lMyMainMenu.Items.Count + 1]);
      lMyMenuItem.Caption := lMyMenuItem.Name;
      //
      lMyMainMenu.Items.Add(lMyMenuItem);
    except
      on E: Exception do
        { what to do? }
          ;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  Button1.Enabled := False; // 2x NOT!!!
  //
  for i := 0 to 3 do
    prcCreateMenuItems;
  //
  if (lMyMainMenu.Items.Count > 0) then
    Self.Menu := lMyMainMenu; // show the menu on Form1
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // MainMenu.Items.Clear:
  // ----------
  // Removes and frees all menu items listed in the Items property.
  // Use Clear to free all the items listed in the Items property array.
  //
  if not(lMyMainMenu = nil) then
    lMyMainMenu.Items.Clear;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  lMyMainMenu := TMainMenu.Create(Self); // Self = Form1
  //
  ShowMessage('MainMenu Items = ' + lMyMainMenu.Items.Count.ToString); // = "0"
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if not(lMyMainMenu = nil) then
    lMyMainMenu.Free;
end;

initialization

ReportMemoryLeaksOnShutdown := true;

finalization

end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 zhyhero (zhyhero) ★☆☆☆☆ -
盒子活跃会员
2020/11/27 13:18:05
3楼: 我觉得这里有两个问题
1:  
  Menu.Items.Count 不等于 Menu.ComponentCount 。
  当放置一个空的Menu控件到窗体上时
  Menu.Items.Count值为0
  Menu.ComponentCount值为1  
  Component[0]就是我说的那个index为0的空白MenuItem

2:
  Menu.Items.Count 只代表顶层项目的数量,不包含每个项目项目的子项目的数量
----------------------------------------------
z@S7
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2020/11/28 2:55:46
4楼: Sorry! my Google Translater!

in TMainMenu, "FItems" is a TMenuItem
in TMenuItem, "FItems" is a TList

look, that "FItems" is created on "TMainMenu.Create" and never is deleted, because a MainMenu (to show itself) needs "Items"

And, the "Items" should have a "Caption" to appears on "MainMenu". 
Then, the "MainMneu appears on "Form"

Complex this logical!!!

No talk about "Assembly code" involved in this task!
----------

  object MainMenu1: TMainMenu
    Left = 272
    Top = 104
    object TMenuItem  // in Design-Time and with no Caption
    end
  end


function TMenu.CreateMenuItem: TMenuItem;
begin
  Result := TMenuItem.Create(Self);
end;

destructor TMenu.Destroy;
begin
  FItems.Free;
  FImageChangeLink.Free;
  inherited Destroy;
end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行80.07813毫秒 RSS