对 Delphi 中的运行时和设计时包大惊小怪
我发现 Delphi 中的大多数组件(VCL)都分为两部分。
1) 设计时包
2) 运行时包
为什么这么大惊小怪。 如果 RunTime 和 DesignTime 包合并为一个包,会有什么区别?
我一直无法真正理解这种分离逻辑。
那么这背后的逻辑是什么呢?
有一次我领导有人提到这种区别只是为了避免采用和遵循微软制定的组件标准。 这背后确实没有逻辑。
这是真的?
I have seen that most of the components (VCLs) in Delphi are split in two parts.
1) DesignTime Package
2) RunTime Package
Why all this fuss. What difference does it make if both RunTime and DesignTime packages are united into one single Package?
I have never really been able to understand this separation logic.
So what is the logic behind this?
Once I had head someone mention that this distinction was made just to avoid adopting and following Component standards as laid down by Microsoft. Really there is no logic behind this.
Is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
答:某些组件具有大型且复杂的设计时功能,例如属性编辑器,您可能不希望将其包含在运行时应用程序中。
B. 一些组件供应商不想将其大型且复杂的设计时功能授权给免版税的运行时使用,而是限制它们仅供开发人员使用。
A. Some components have large and complex design-time features such as property editors, which you may not want to include in your run-time application.
B. Some component vendors do not want to licence their large and complex design-time features for royalty-free run-time use, but restrict them to use by developers only.
您要访问的内部单元/包
既没有源代码也没有
合法地允许分发
二进制形式。
想让你的应用程序需要 Delphi
要安装在用户的
电脑。
其逻辑是将您自己的代码与“粘合”代码分开,这使得它变得漂亮且易于使用。 易于在 IDE 中使用。
internal units/packages to which you
neither have source code nor are
legally allowed to distribute in
binary form.
want to make your application require Delphi
to be installed on the user's
computer.
The logic is to keep your own code separate from the "glue" code which makes it nice & easy to work with in the IDE.
如果您做了一些研究,您会发现 这个SO问题在不到2天前提出...
正如已经解释的那样,主要原因是您不能在运行时包中包含任何Delphi设计单元。 而且没有理由使用只能在 IDE 中运行的代码来膨胀您的可执行文件。
If you had done a little bit of research, you'd have found this SO question asked less than 2 days ago...
As already explained the main reason is that you cannot include any Delphi Design unit in a runtime package. And there is no reason to bloat your executable with code that can only run within the IDE anyway.
简短的答案是...如果你想正确地做到这一点,它会很复杂并且有一个缺点。
不那么简短的答案是...使用懒人的解决方案:运行时/设计时混合。 还带有一个可能(但不太可能)的缺点。
完整的答案是:
无论我们安装什么库,它都有两种“风格”:运行时库和设计时库(实际上,有三种......继续阅读)。
运行时库
某些库仅提供运行时例程,不提供可视化组件 (VCL)。
现在,我们可以简单地从我们的程序中调用该库的函数。
如果库有两个包(两个 DPK 文件),一个标记为设计时,一个标记为运行时,请始终在设计时库之前编译运行时库。 请注意,有时名称末尾只有一个“D”(设计时)和一个“R”(运行时)。
如果我们从设计时库开始,它可能会抱怨找不到运行时库。
设计时库
提供可视化组件的库是设计时库*。 我们需要在 Delphi 中加载这些库(进入项目管理器)并安装它们。
安装后,该库的可视化组件将出现在“调色板”中,以便我们可以在设计时将它们拖放到表单上。
该库的文档将告诉我们在哪个类别下可以找到新安装的组件。 如果没有文档,请在该库的单元中搜索名为“注册”的过程。 这将采用两个字符串作为参数:组件的名称和它将出现在“Palette”中的类别:
TRichLog 组件将出现在面板中的“Cubic”类别下。
如果我们不再需要该库,我们可以在“项目管理器”中右键单击它并选择“卸载”。
设计时和运行时库
有些人可能不同意我上面的说法。 有关详细信息,请参阅这些页面:
RobsTechcorner.blogspot.com/2011/06/runtimedesigntime-what-delphi-packages.html
这是因为,理论上,设计时包应该只包含属性编辑器等特殊内容的代码(您可以在对象检查器中看到属性编辑器) 。 并且运行时包应该只包含开发过程中不需要的代码。 运行时包不应包含或需要设计时代码。
这就是理论。 编程和保持代码分离的好方法。 然而,这是有代价的:将两者分开可能很困难。
解决方案是将您的包声明为混合的“设计时和运行时库”。
顺便说一下,当使用向导创建一个新包时,Delphi 会默认将该包声明为“设计时和运行时”。 但是,这仍然被认为是懒人的解决方案。
老实说,我只使用这个解决方案。 尽管我创建了很多可视化组件,但没有一个具有设计时编辑器。
The short answer is... If you want to do it properly, it's complicated AND comes with a drawback.
The not so short answer is... use lazy man's solution: the runtime/design time mix. Also comes with a possible (but unlikely) drawback.
The full answer is:
Whatever library we install, it comes in two “flavors”: runtime libraries and design time libraries (actually, there are three… keep reading).
Run-time libraries
Some libraries only provide run-time routines and no visual components (VCL).
Now, we can simply call the functions of that library from our program.
If the library has two packages (two DPK files), one marked as Design time and one marked as Runtime, always compile the Runtime library before the Design time library. Note that sometimes there is just a “D” (Design time) and an “R” (Runtime) at the end of the name.
If we start with the Design time library, it may complain that it cannot find the Runtime library.
Design time libraries
Libraries that provide visual components are design-time libraries*. We need to load these libraries in Delphi (into the Project Manager) and install them.
After the installation, the visual components of that library will appear into the “Palette” so we can drag and drop them on our form, at design time.
The documentation of the library will tell us under which category we will find the new installed components. If there is no documentation, do a search into the units of that library for a procedure called “Register”. This will take as parameter two strings: the name of the component and the category where it will appear in the “Palette”:
The TRichLog component will appear under the “Cubic” category in the Palette.
If we don’t want the library anymore, we right-click on it in “Project manager” and choose “Uninstall”.
Design time and Run time libraries
Some people might not agree with my statements above. See these pages for details:
RobsTechcorner.blogspot.com/2011/06/runtimedesigntime-what-delphi-packages.html
This is because, in theory, design time packages should only contain code for special things like property editors (you can see property editors in the Object Inspector). And the runtime packages should only contain code that is not necessary during development. Runtime packages should never contain or require design time code.
This is the theory. The good way to program and keep your code separated. However, this comes at a price: it could be difficult to keep these two separated.
The solution is to declare your packages as mixed “Design time and Run time libraries”.
By the way, when using the wizard to create a new package, Delphi will declare that package as “design time and runtime” by default. But still, this is considered, lazy man’s solution.
To be honest, I only use this solution. Even though I created lots of visual components, none has design time editors.