【问题标题】:Creating Tabs, Groups, and ActionItems on TRibbon at runtime using Delphi XE-5使用 Delphi XE-5 在运行时在 TRibbon 上创建选项卡、组和操作项
【发布时间】:2014-01-16 00:28:51
【问题描述】:

我之前发布了这个,但删除了它,因为我认为我可以贡献更多,但很快意识到我不能。无论如何,我需要在运行时创建选项卡、组和操作项。

procedure TfrmMain.Button2Click(Sender: TObject);
var
 Tab: TRibbonTabItem;
 Group: TRibbonGroup;
 //Item: TActionItem;
begin
 Tab:= Ribbon1.Tabs.Add;
 Tab.Caption:= 'Test';
 Group:= TRibbonGroup.Create(Tab.Page);
 //Tab.Page.AddGroup(Group);
 Group.Caption:= 'Test Group';
 //create actionitem on group
end;

任何帮助将不胜感激。我找不到任何好的资源来展示如何在运行时完成此操作

每个选项卡的所有选项卡和组似乎都是在没有任何问题的情况下创建的。我似乎无法正确创建按钮。下面是要在每个组上创建的按钮的代码。我有一条显示消息,我在进入此 for 循环之前查看按钮的数量,并且数字始终正确。然而,组上似乎只显示了一个按钮。

   //ShowMessage(IntToStr(ButtonNames.Count));
   for K:= 0 to ButtonNames.Count-1 do
   begin
    BarItem := ActionManager1.ActionBars.Add;
    BarItem.ActionBar := RGroup;
    BarAction := BarItem.Items.Add;
    BarAction.Caption := ButtonNames[K];
   end; //for K:= 0 to ButtonNames.Count-1 do

以下是用于创建整组选项卡、组和按钮的完整源代码

procedure TfrmMain.Button1Click(Sender: TObject);
var
 I, J, K: Integer;
 JSONData, TabNames, GroupNames, ButtonNames: TStringList;
 NavData: TJSONObject;
 Tabs, Groups, Buttons, Tab, Group, Button: TJSONValue;
 TabItem: TRibbonTabItem;
 RGroup: TRibbonGroup;
 BarItem: TActionBarItem;
 BarAction: TActionClientItem;
begin
 JSONData:= TStringList.Create;
 if openDialog1.Execute then
 begin
  JSONData.LoadFromFile(openDialog1.FileName);
  try
    NavData:= getJSONObj(JSONData.Text);

    if NavData <> nil then
    begin
     {Ribbon1:= TRibbon.Create(self);
     Ribbon1.Parent:= Self;

     ActionManager1:= TActionManager.Create(self);
     Ribbon1.ActionManager:= ActionManager1;}

     Tabs:= getTabs(NavData);
     TabNames:= TStringList.Create;
     getTabNames(Tabs, TabNames);
     for I:= 0 to TabNames.Count-1 do
     begin
      TabItem := Ribbon1.Tabs.Add;
      TabItem.Caption := TabNames[I];
      Tab:= getTabByName(Tabs, TabNames[I]);
      Groups:= getGroups(TJSonObject(Tab));
      GroupNames:= TStringList.Create;
      getGroupNames(Groups, GroupNames);
      for J:= 0 to GroupNames.Count-1 do
      begin
       RGroup := TRibbonGroup.Create(Ribbon1);
       RGroup.Parent := TabItem.Page;
       RGroup.Caption := GroupNames[J];

       Group:= getGroupByName(Groups, GroupNames[J]);
       Buttons:= getButtons(TJSonObject(Group));
       ButtonNames:= TStringList.Create;
       getButtonNames(Buttons, ButtonNames);
       //ShowMessage(IntToStr(ButtonNames.Count));
       for K:= 0 to ButtonNames.Count-1 do
       begin
        BarItem := ActionManager1.ActionBars.Add;
        BarItem.ActionBar := RGroup;
        BarAction := BarItem.Items.Add;
        BarAction.Caption := ButtonNames[K];
       end; //for K:= 0 to ButtonNames.Count-1 do
      ButtonNames.Free;
      end; //for J:= 0 to GroupNames.Count-1 do
      GroupNames.Free;
     end;
     TabNames.Free;
    end;
  finally
    NavData.Free;
  end;
 end;
 JSONData.free;
end;

【问题讨论】:

  • 按钮、图标、群组中的任何内容。我在互联网上看到一个帖子提到了actionItems。如果我能得到显示组的选项卡,我会很高兴,但在组上显示东西也会很好。我需要可点击的项目。
  • 话虽如此,我认为还有比我现在要做的更大的问题,我将发布第二个问题
  • 您已在posted one 回复您,并提供了一个简单代码的链接,该代码显示了如何使用组和该组中的按钮创建选项卡。我以为你在删除帖子之前已经看到了...
  • 有时你需要等待答案。有时您可能会阅读 cmets... 没关系,假设您的意思是来自操作管理器的操作,您可以使用代码 like this
  • 您从原始帖子中提出了一个不同的问题,所以我会在评论中再次回答您:-) 您不能将多个操作栏添加到一个组中。并且由于您尝试在循环的每次迭代中将操作栏添加到同一组中,因此先前创建的操作栏会被新的操作栏“覆盖”。你当然是想写它like this

标签: delphi delphi-xe5 ribbon-control


【解决方案1】:

我发现 TLama 发布的答案很好,并且不在本站内。

这个问题应该答对了,所以我添加答案,如果TLama想添加它有他的答案,请不要删除这个。

要将运行时操作添加到功能区菜单,您需要有一个组。 该组需要连接到单个 barItem(并且 bar 项连接到该组)。 为菜单上的每个新按钮添加 BarAction。

procedure TForm1.Button1Click(Sender: TObject);
var
  Group: TRibbonGroup;
  TabItem: TRibbonTabItem;
  BarItem: TActionBarItem;
  BarAction: TActionClientItem;
begin
  TabItem := Ribbon1.Tabs.Add;
  TabItem.Caption := 'Tab caption';

  Group := TRibbonGroup.Create(Ribbon1);
  Group.Parent := TabItem.Page;
  Group.Caption := 'Group caption';

  BarItem := ActionManager1.ActionBars.Add;

  BarAction := BarItem.Items.Add;
  BarAction.Action := Action1;
  BarAction.Caption := 'Action 1 caption';

  BarAction := BarItem.Items.Add;
  BarAction.Action := Action2;
  BarAction.Caption := 'Action 2 caption';

  BarItem.ActionBar := Group;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多