如何在Delphi 2010中以编程方式向Action Manager添加操作 [英] How do I programatically add actions to an Action Manager in Delphi 2010

查看:113
本文介绍了如何在Delphi 2010中以编程方式向Action Manager添加操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态添加操作项,我可以添加该项目,并且在执行此操作时会起作用:

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;

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

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;

但是ActionComponent始终为零。为什么未初始化ActionComponent?

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

推荐答案

简短答案:

您希望 TActionClientItem 显示为<$ c的 ActionComponent $ c> TAction 。这不会发生,因为 TActionClientItem 不是来自 TComponent

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

更长的答案:

我相信您正在将项目添加到菜单栏中。似乎是设计使然,链接到菜单项的 TAction 不支持 ActionComponent 。菜单栏的项目类型为 TActionClientItem 。这是一个收藏品,而不是一个组件。因此,在调用 ActionComponent 参数。所选项目的操作链接的HelpUpdate2 / EN / html / delphivclwin32 / Classes_TBasicActionLink_Execute.html rel = nofollow> Execute 方法。如果这听起来令人困惑,我想VCL源代码中的以下引号会很清楚:

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 方法:

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

传递的组件分配给 FAction.ActionComponent

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

如何从 TCustomActionMenuBar.ExecAction 调用:

FSelectedItem.ActionLink.Execute;

对于标题中的问题,我认为您没有做错任何事情,除了无需设置 TActionClientItem 标题 ImageIndex ,因为它是将显示的 TAction 的标题和图像。

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.

这篇关于如何在Delphi 2010中以编程方式向Action Manager添加操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆