如何以编程方式将操作添加到 Delphi 2010 中的操作管理器

发布于 2024-10-04 03:30:51 字数 524 浏览 4 评论 0原文

我正在尝试动态添加操作项,我可以添加该项目,并且当我这样做时它会起作用:

HostActionItem := ActionManager.ActionBars[0].Items[0].Items[2];
  NewItem := HostAction.Items.Add;
  NewItem.Action :=  MyActionToPerform;
  NewItem.Caption := Description;
  NewItem.ImageIndex := 1;
  NewItem.Tag := 13;

但是,当操作 Execute 方法触发时,我尝试从 Sender 对象获取 ActionComponent,如下所示:

  if (Sender is TAction) then
  tag := (Sender As TAction).ActionComponent.Tag;

但 ActionComponent 始终为零。为什么 ActionComponent 没有被初始化?

I am trying to dynamically add actionitems, I can add the item and it works when I do this:

HostActionItem := ActionManager.ActionBars[0].Items[0].Items[2];
  NewItem := HostAction.Items.Add;
  NewItem.Action :=  MyActionToPerform;
  NewItem.Caption := Description;
  NewItem.ImageIndex := 1;
  NewItem.Tag := 13;

However, when the action Execute method fires I attempt to get the ActionComponent from the Sender object like this:

  if (Sender is TAction) then
  tag := (Sender As TAction).ActionComponent.Tag;

But the ActionComponent is always nil. Why is the ActionComponent not being initialised?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-10-11 03:30:51

简短回答

您期望 TActionClientItem 显示为 TActionActionComponent。这种情况不会发生,因为 TActionClientItem 不是从 TComponent 派生的。

更长的答案:

我相信您正在将项目添加到菜单栏。链接到菜单项的 TAction 似乎是有意设计的,不支持 ActionComponent。菜单栏的项目的类型为 TActionClientItem。这是一个“收藏品”,而不是一个“组件”。因此,在调用 所选项目的操作链接的执行方法。如果这听起来令人困惑,我想下面来自 VCL 源代码的引用会清楚地说明:

TBasicActionLink.Execute 方法:

function Execute(AComponent: TComponent = nil): Boolean; virtual;

传递的组件在它被分配之前被分配给 FAction.ActionComponent被执行。

如何从 TCustomActionMenuBar.ExecAction 调用:

FSelectedItem.ActionLink.Execute;

对于标题中的问题,除了设置 Caption之外,我认为您没有做任何错误的事情TActionClientItem 的 ImageIndex 是不必要的,因为将显示 TAction 的标题和图像。

short answer:

You're expecting a TActionClientItem to show up as ActionComponent of an TAction. That won't happen since TActionClientItem does not descend from TComponent.

longer answer:

I believe you're adding your item to a menu bar. It seems to be by design that an TAction linked to a menu item would not support the ActionComponent. The items of a menu bar is of type TActionClientItem. This is a 'collection item', not a 'component'. Hence the menu cannot fill in the ActionComponent parameter with the menu item when calling the Execute method of the action link of the selected item. If this sounds confusing, I guess the below quotes from the VCL source would make it clear:

TBasicActionLink.Execute method:

function Execute(AComponent: TComponent = nil): Boolean; virtual;

The passed component is assigned to FAction.ActionComponent before it is executed.

How it's called from TCustomActionMenuBar.ExecAction:

FSelectedItem.ActionLink.Execute;

For the question in the title, I don't think you're doing anything wrong, apart from setting the Caption and ImageIndex of a TActionClientItem is unnecessary, as it's the TAction's title and image which will be shown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文