如何在组件中添加对操作的支持
我需要做什么才能向我的组件添加操作支持。它是一个按钮组件,但我想无论它是什么组件类型,它都是相同的。任何信息或如何操作都会有帮助。
What do I need to do for adding actions support to my component. It is a button component but I guess it is the same for whatever component type it is. Any information or how to will help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您如何定义行动支持。有两种类型:
动作属性
每个 TControl 后代都有一个 Action 属性,默认情况下该属性的执行链接到鼠标左键单击。此链接由 ActionLink 管理。默认的 ActionLink 是 TControlActionLink 类型,它负责 Action 和 Control 的标题、提示、启用状态等的同步。如果这个基本功能就是您想要的,那么只需在组件类型声明中发布 Action 属性,Delphi 框架就会处理所有事情,例如 Serg 和 LU RD 已回答。
如果您希望自己的 Action 属性链接到某些其他条件或事件(即,除了 Click 之外),或者如果您想为组件的特定子元素(不是 TControl 后代)实现 Action 属性,那么您可以通过定义和实现自定义 ActionLink 类来实现您自己的自定义 Action 属性。
假设您的组件是某种具有列的网格,并且您希望每个列都有一个操作属性,当用户单击列的标题时应该调用该属性。由于此类列可能是 TCollectionItem 类型,因此默认情况下列类型没有操作属性。所以你必须自己实现一个。考虑下一个示例,它将操作的标题链接到列的标题,将操作的启用状态反向链接到列的只读属性等等...:
请注意,此代码仅被剥离到适用的操作部分。
来源:www.nldelphi.com。
动作组件
动作组件可分配给任意组件的动作属性。但由于编写这样一个操作组件所涉及的所有内容的解释都非常全面,因此我将通过提供下面的示例来让自己变得容易。
假设您想要创建一个提供缩放功能的控件,并且还想要可以分配给工具栏按钮的相应 ZoomIn 和 ZoomOut 操作。
激活此操作(通过单击工具栏按钮或选择菜单项)会按以下优先级调用 ZoomIn 例程:
随后,只需添加 ZoomOut 操作:
请注意,操作组件需要在 IDE 中注册才能在设计时使用它们。
Delphi 帮助中适用的阅读内容:
来源:www.nldelphi.com。
That depends on how you define action support. There is two kinds:
An action property
Every TControl descendant has an Action property which execution is by default linked to a left mouse button click. This link is managed by an ActionLink. The default ActionLink is of the type TControlActionLink which takes care of the synchronization of the caption, the hint, the enabled state, etc... of both the Action and that of the Control. If this basis functionality is all that you want, then simply publish the Action property in your component type declaration and the Delphi framework takes care of all, like Serg and LU RD already answered.
If you want your own Action property to be linked to some other condition or event (i.e. other than Click), or if you want to implement an Action property for a specific sub element of your component (that is not a TControl descendant), then you can implement your own custom Action property by defining and implementing a custom ActionLink class.
Suppose your component is some kind of grid which has columns and you want every column to have an action property that should be invoked when the user clicks the title of a column. Since such columns are likely to be of a TCollectionItem type, the column type does not have an action property by default. So you have to implement one yourself. Consider the next example which links the action's caption to the column's title, links the action's enabled state inversely to the column's readonly property and so on...:
Note that this code is stripped to only the applicable action parts.
Source: www.nldelphi.com.
An action component
An action component is assignable to the action property of an arbitrary component. But since explaining all that is involved with writing such an action component is pretty comprehensive, I will make it easy for myself in providing the example below.
Suppose you want to make a control that provides zoom capabilities and that you also want the corresponding ZoomIn and ZoomOut actions that can be assigned to toolbar buttons.
Activating this action (with a click on a toolbar button, or choosing a menu item) calls in the following priority the ZoomIn routine of:
Subsequently, the ZoomOut action is simply added:
Note that action components require registration in the IDE for being able to use them design time.
Applicable read food in the Delphi help:
Source: www.nldelphi.com.
基本操作支持是在 TControl 类中实现的,因此在最简单的情况下,您所要做的就是从 TControl 后代继承您的组件并将
Action
属性声明为已发布,例如:如果您的组件有其他属性应该链接到 TAction 属性,您还应该重写 ActionChange 方法。
Basic action support is implemented in TControl class, so in the most simple case all you have to do is to inherit your component from TControl descendant and declare
Action
property as published, ex:If your component has additional properties that should be linked to TAction properties you should also override ActionChange method.
如果您的组件已经是 TButton 的后代,那么操作支持将被继承。
您所需要做的就是将操作属性声明为已发布。
If your component is already a descendant of TButton then the action support is inherited.
All you need to do is declare the action property as published.