请问如何遍历TMainMenu组件,找出全部的子菜单项的Caption,放入一维数组中?

解决方案 »

  1.   

    procedure GetMenuName(MainMenu: TMainMenu; MenuItem: TMenuItem; StrList: TStringList);
    var
      Item: TMenuItem;
      Count, I: Integer;
    begin
      if MenuItem = nil then
        begin
          Count := MainMenu.Items.Count;
          if Count = 0 then exit;
          for I := 0 to Count - 1 do
            begin
              Item := MainMenu.Items[I];
              StrList.Add(Item.Caption);
              if Item.Count > 0 then
                GetMenuName(MainMenu, Item, StrList);
            end;
        end
      else
        begin
          Count := MenuItem.Count;
          if Count = 0 then exit;
          for I := 0 to Count - 1 do
            begin
              Item := MenuItem.Items[I];
              StrList.Add(Item.Caption);
              if Item.Count > 0 then
                GetMenuName(MainMenu, Item, StrList);
            end;
        end
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      StrList: TStringList;
    begin
      try
        StrList := TStringList.Create;
        StrList.Capacity := 256;    GetMenuName(MainMenu1, nil, StrList);    //...
      finally
        FreeAndNil(StrList);
      end;
    end;
      

  2.   

    WGYKING(修罗是谁?!)又把我要说的说了!
      

  3.   

    这几天有事没在网上,谢谢WGYKING(修罗是谁?!)的程序,我来试一下!
      

  4.   

    谢谢WGYKING(修罗是谁?!):谢谢,问题解决!我结贴了。有空再联系。