组件如何在设计时确定项目目录
我编写了一个组件,它应该存储一些与项目目录相关的信息。每次更改组件的属性时,它都应该写入一个文件。那么组件如何在设计时确定当前项目目录呢?
提前致谢
编辑:
我想在每次更改组件的属性时生成一个 delphi 源文件,以便在编译代码时始终获得最新版本。将其视为一种代码生成器。
目前,我设置了应存储源的完整路径和文件名,但我更喜欢项目的相对路径(或包含我的组件的表单/数据模块),以便更轻松地在不同的开发人员计算机上复制项目。
I write a component which should store some information relative to the project directory. Every time a property of my component is changed it should write a file. So how can a component determine the current project directory at design time.
Thanks in advance
EDIT:
I want to generate a delphi source file every time a property of my component is changed, so that I always get the latest version when I compile my code. Think of it as a kind of code generator.
At the moment I set whole path and filename where the source should be stored but I prefer a relative path to the project (or the form/datamodule which contains my component) to make it easier to copy the project on different developer machines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
感谢您的提示。开放工具 API 是可行的方法,并且可以在设计时从表单上的组件使用开放工具 API。
这是我的解决方案:
我需要两个单元,一个用于组件,另一个用于注册组件和使用开放工具 API 的代码。
这是组件单元:
这是注册组件和调用 Open Tools API 的单元:
Thanks for the hints. Open Tools API is the way to go and using the Open Tools API from a component on a form at designtime is possible.
So here is my solution:
I need two units, one for the component and one for registering the component and the code which use the Open Tools API.
Here comes the component unit:
Here is the unit for registering the component and the call to the Open Tools API:
从delphi 7开始,ToolsAPI单元定义了一个getActiveProject函数,它返回当前项目的IOTAProject接口。
IOTAProject 的 fileName 属性返回项目主源文件(通常是 .dpr 文件)的完整路径。
因此,在许多情况下,可以使用简单的指令,例如:(
并且不需要像上面亨氏的示例那样使用两个单位)
Starting with delphi 7, the ToolsAPI unit defines a getActiveProject function which returns the IOTAProject interface of the current project.
The fileName property of IOTAProject returns the full path of the main source file of the project (typically the .dpr file).
So, in many cases, it's possible to use a simple instruction such as:
(and there's no need of using two units as in Heinz's example above)
组件无法访问您的源路径,因为组件被放置在您的应用程序中并作为应用程序的一部分在 Delphi IDE 之外运行。
如果您想访问项目路径,或者自动化 IDE 内的任何流程;你必须使用 OpenTools API 编写一个 IDE 专家,而不是一个组件。
A component cannot access your source path, because a component is placed in your application and run as a part of your application out of Delphi IDE.
If you want to have access to project path, or automate any process inside IDE; you have to write an IDE expert using OpenTools API, not a component.
我认为不可以。您可以很容易地确定 EXE 运行的目录,但设计时的组件是作为 IDE 的一部分运行的。我怀疑组件是否有办法通过 IDE 访问项目信息。
I don't think it can. You can determine the directory your EXE is running in easily enough, but your component at design-time is running as a part of the IDE. I doubt there's a way for the component to access project information through the IDE.
请参阅
http://www.gexperts.org/otafaq.html#project
,然后
www.href.com/pub/sw/ProjectOptions.html
可能有帮助
see
http://www.gexperts.org/otafaq.html#project
and then
www.href.com/pub/sw/ProjectOptions.html
may be it helps
我有一个有点不同的目标:如何知道项目 exe 在设计时的位置,以便应用程序在运行时可以与设计时功能共享一些文件。
因此,我结合了两个最佳答案,并创建了以下要在应用程序中使用的函数:
然后在组件注册单元中(我将其分开并仅链接到设计时包):
从而避免实际单元依赖于 ToolsAPI 。
MyUtils.GetProjectTargetPath 在运行时返回一个空字符串,实际上对应于应用程序的 exe 目录。
I had A little bit different objective: how to know where the project exe will be at design time, so that the application at runtime can share some files with the design time functionality.
So, I combined the two best answers and created the following function to be used in applications:
and then in the component registration unit (which I have separate and linked only to the design time package):
Thus avoiding the actual unit to depend on ToolsAPI.
MyUtils.GetProjectTargetPath returns an empty string at runtime, which corresponds to the application exe-directory in practice.