Delphi VCL for Win32 - 多个事件处理程序

发布于 2024-08-05 00:46:15 字数 309 浏览 6 评论 0原文

我正在寻找一些代码,可以轻松地将许多事件处理程序分配给对象的单个事件...我的插件架构非常需要它,所以我实现了一些替代方案,但是我不喜欢我的解决方案,所以我想知道你的想法/解决方案/提示是什么...我的解决方案只是一个包含以下项目的列表 事件名称:字符串; 过程:TMyEventProc; 其中 TMyEventProc 有两个参数:sender 和 eventData: 指针。 根据事件的名称,eventData 指向不同的记录/对象。

不幸的是,这需要声明很多很多记录才能作为参数传递。 该方法也非常慢,并且需要在“真实”事件发生时调用所需的“回调”。

I'm looking for some code allowing easy asigning many event handlers to a single event of object... I needed it very much for my plugin architecture, so I implemented some replacement for that, however I don't like my solution for that, so I'd like to know what is yours idea/solution/tip ... My solution is just a list with items like
eventName: string;
proc: TMyEventProc;

where TMyEventProc takes two arguments, sender and eventData: pointer.
depending on name of the event, eventData points to different record / object.

Unfortunately this requires declaration of many, many records for being passed as argument.
The method is also very slow, and requires to implement calling the required "callbacks" while the "real" event gets hit.

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

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

发布评论

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

评论(4

硬不硬你别怂 2024-08-12 00:46:15

我实现了一个解决方案,它适用于几乎任何版本的 Delphi - 它最初是在 Delphi 7 中实现的,尽管我没有在早期版本中测试过它(但如果您自己使用 Delphi 7,那么这就是您所需要的知道,对吧?)。 :)

iirc 这至少部分是 Allen Bauer 帖子的灵感来源。您可以在一些视频中观看我的实现演示,并从我的博客下载代码

您所在的帖子感兴趣的被标记为“多播”。最终代码的下载链接可在本文中找到

在我的方法中,您从 TMultiCastEvent 派生一个类。您的派生类所要做的就是实现一些简单的类型安全保护,以添加、删除和调用具有特定签名的事件。

TNotifyEvent 的实现 - procedure(Sender: TObject) - 提供了“让您继续前进”的实现(大多数“有用”的多播事件是简单的通知),并且还作为如何为特定的多播事件派生类的示例。事件签名。

一旦您有了多播事件类,您就可以与多播版本互换使用常规“事件处理程序”,例如,给定一些具有多播 On_Click 事件的虚构按钮类(我采用了在事件中插入下划线的约定)名称以将其标识为多播,而不是常规的“单播”事件):

将处理程序分配给单播单击事件的代码:

  Button.OnClick := MyClickHandler;

可以直接将相同的处理程序添加到多播通知事件:

  MultiCastButton.On_Click.Add(MyClickHandler);

我的实现还包括许多改进,例如在实现对象被销毁时禁用事件并自动从处理程序中删除处理程序的能力(这涉及少量的内务处理,如有必要可以忽略,但在某些情况下可能有用)。

所有这些都在我的博客文章中进行了描述和演示。

享受。 :)

I implemented a solution to this that works in just about any version of Delphi - it was originally implemented in Delphi 7, although I haven't tested it in earlier versions (but if you're using Delphi 7 yourself, then that's all you need to know, right?). :)

iirc this was at least in part the inspiration for Allen Bauer's post. You can see my implementation demonstrated in some videos and download the code from my blog:

The posts you are interested in are tagged "multicast". The download link for the final code is available in this post.

In my approach, you derive a class from TMultiCastEvent. All your derived class has to do is implement some simple type safety protection for adding, removing and invoking an event with a specific signature.

An implementation for TNotifyEvent - procedure(Sender: TObject) - is provided with the implementation both "to get you going" (ime most "useful" multicast events are simple notifications) and also as an example of how to derive multicast event classes for specific event signatures.

Once you have your multicast event class you can use regular "event handlers" interchangeably with the multi-cast version, e.g. given some imaginary button class with a multi-cast On_Click event (I've adopted a convention of interposing an underscore in the event name to identify it as multicast, vs regular "uni-cast" events):

Code that assigns a handler to a unicast click event:

  Button.OnClick := MyClickHandler;

Can directly add that same handler to a multi-cast Notify event:

  MultiCastButton.On_Click.Add(MyClickHandler);

My implementation also includes a number of refinements, such as the ability to disable events and have handlers automatically removed from handlers when the implementing object is destroyed (this involves a small amount of housekeeping which can be ignored if necessary but which can be useful under certain circumstances).

All of which is described and demonstrated in my blog posts.

Enjoy. :)

蓝眼睛不忧郁 2024-08-12 00:46:15

Allen Bauer 有一篇关于多播事件的博客文章,可能会有所帮助。不过,它仅适用于 Delphi 2009 或更高版本。

编辑:如果您仍在 D7 上,并且不需要太多不同的事件签名,您仍然可以完成这项工作。尝试查看 Allen 的代码,看看是否可以将其改编为非通用解决方案。

Allen Bauer has a blog post about multicast events that might be helpful. It only works for Delphi 2009 or later, though.

EDIT: If you're still on D7, you might still be able to make this work if you don't need too many different event signatures. Try looking at Allen's code and see if you can adapt it to a non-generic solution.

放血 2024-08-12 00:46:15

我认为,如果您正在实现一个插件系统,您就不能仅仅使用事件处理程序——无论是否是多播。我建议查看观察者模式。在多播事件附近可能听起来有点过于冗长,但至少在您需要时更加灵活。

If you're implementing a plugin system, I think, you can't just get away with event handlers - be it multicast or not. I suggest having a look at observer pattern. Might sound a little bit too verbose near multicast events but at least more flexible when you need.

淡淡離愁欲言轉身 2024-08-12 00:46:15

您可以使用观察者设计模式来实现这一点。下面是一个 Delphi 实现示例: http://blogs.teamb.com/joannacarter /2004/06/30/690

You can use the observer design pattern for that. Here is a sample delphi implementation: http://blogs.teamb.com/joannacarter/2004/06/30/690

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