使用Delphi XE-5在运行时在TRibbon上创建选项卡,组和ActionItem

时间:2014-01-16 00:28:51

标签: delphi delphi-xe5 ribbon-control

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

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;

1 个答案:

答案 0 :(得分:0)

我发现TLama发布的答案很好,并且在本网站之外。

这个问题应该得到适当的回答,所以我添加了答案,如果TLama想要添加它有他的答案,请做好并删除这个。

要在功能区菜单上添加运行时操作,您需要拥有一个组。 该组需要连接到单个barItem(并且条形项连接到该组)。 对于菜单上的每个新按钮,您都可以添加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;