我可以让自定义 Delphi 组件向使用子句添加多个单元吗? [英] Can I make a custom Delphi component add multiple units to the uses clause?

查看:18
本文介绍了我可以让自定义 Delphi 组件向使用子句添加多个单元吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 XE2 中的一些组件.是否可以将超过组件的单元添加到它所放置的表单的使用子句中?

I'm working on some components in XE2. Is it possible to have more than the component's unit added to the uses clause of the form it's dropped on?

示例:当我从我构建和安装的包中选择 TCustomComponent 时,单元 CustomComponent 被添加到表单的 uses 子句中.我还想添加单位 GlobalConstants.

Example: When I choose the TCustomComponent from a package I've built and installed the unit CustomComponent is added to the form's uses clause. I would like to also add the unit GlobalConstants.

有谁知道这是否可以做到?

Does anyone know if this can be done?

推荐答案

为您的组件创建一个设计时包(如果您还没有).在该包中,创建一个实现 ISelectionEditor 的类 接口(最简单的方法是从 TSelectionEditor 类),覆盖其虚拟 RequiresUnits() 方法报告您希望出现在使用您的组件的任何 Form/Frame/DataModule 的 uses 子句中的任何其他单元.然后,让你的包的 Register() 函数通过调用 RegisterSelectionEditor()(除了 RegisterComponents()).

Create a design-time package for your component (if you do not already have one). In that package, create a class that implements the ISelectionEditor interface (the easiest way is to derive from the TSelectionEditor class), overriding its virtual RequiresUnits() method to report any additional units you want to appear in the uses clause of any Form/Frame/DataModule that uses your component. Then, have your package's Register() function register that class by calling RegisterSelectionEditor() (in addition to RegisterComponents()).

Indy 10 正是为其 TIdTCPServerTIdCmdTCPClientTIdUDPServer 组件,以确保将 IdContextIdSocketHandle 单元添加到 uses 子句中.查看 IdCoreSelectionEditors.pasIdRegisterCore.pas 单元,了解 Indy 是如何实现这一点的.

Indy 10 does exactly this for its TIdTCPServer, TIdCmdTCPClient, and TIdUDPServer components, to make sure the IdContext and IdSocketHandle units get added to uses clauses. Look at the IdCoreSelectionEditors.pas and IdRegisterCore.pas units to see how Indy implements this.

更新:IdCoreSelectionEditors.pasIdProtocolsSelectionEditors.pas 单元已于 2014 年 3 月从 Indy 10 中删除.每个组件的所有SelectionEditor 类被替换为在 IdRegisterCore 单元本身中实现的新单个类.这个新的 SelectionEditor 类为所有 Indy 组件注册,它查找在设计时放置的任何 Indy 组件的所有实例,使用所有参数的数据类型的 RTTI 和任何分配的事件处理程序的返回值来了解哪个要报告以包含在 uses 子句中的单位.

Update: the IdCoreSelectionEditors.pas and IdProtocolsSelectionEditors.pas units were removed from Indy 10 in March 2014. All of the per-component SelectionEditor classes were replaced with a new single class implemented in the IdRegisterCore unit itself. This new SelectionEditor class is registered for all Indy components, and it looks for all instances of any Indy component that has been placed at design-time, using RTTI of the data types of all parameters and return values for any assigned event handlers to know which units to report for inclusion in the uses clause.

这篇关于我可以让自定义 Delphi 组件向使用子句添加多个单元吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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