delphi - 组件如何在设计时确定项目目录

标签 delphi delphi-2010 components design-time

我编写了一个组件,它应该存储一些与项目目录相关的信息。每次更改组件的属性时,它都应该写入一个文件。那么组件如何在设计时确定当前项目目录。

提前致谢

编辑:
我想在每次更改组件的属性时生成一个 delphi 源文件,以便在编译代码时始终获得最新版本。将其视为一种代码生成器。

目前,我设置了应存储源的完整路径和文件名,但我更喜欢项目的相对路径(或包含我的组件的表单/数据模块),以便更轻松地在不同的开发人员计算机上复制项目.

最佳答案

感谢您的提示。开放工具 API 是可行的方法,并且可以在设计时从表单上的组件使用开放工具 API。

这是我的解决方案:

我需要两个单元,一个用于组件,另一个用于注册组件和使用开放工具 API 的代码。

这是组件单元:


unit TestLabels;

interface

uses
  SysUtils, Classes, Windows, Controls, StdCtrls;

type
  TTestLabel = class(TLabel)
  private
    FTestProperty: Boolean;
    procedure SetTestProperty(const Value: Boolean);
    procedure Changed;
  published
    property TestProperty: Boolean read FTestProperty write SetTestProperty;
  end;

var
  OnGetUnitPath: TFunc;

implementation

{ TTestLabel }

procedure TTestLabel.Changed;
begin
  if not (csDesigning in ComponentState) then
     Exit; // I only need the path at designtime

  if csLoading in ComponentState then
     Exit; // at this moment you retrieve the unit path which was current before

  if not Assigned(OnGetUnitPath) then
    Exit;

  // only for demonstration
  Caption := OnGetUnitPath;
  MessageBox(0, PChar(ExtractFilePath(OnGetUnitPath)), 'Path of current unit', 0);
end;

procedure TTestLabel.SetTestProperty(const Value: Boolean);
begin
  if FTestProperty  Value then
  begin
    FTestProperty := Value;
    Changed;
  end;
end;

end.

这里是注册组件和调用 Open Tools API 的单元:


unit TestLabelsReg;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, TestLabels;

procedure register;

implementation

uses
  ToolsAPI;

function GetCurrentUnitPath: String;
var
  ModuleServices: IOTAModuleServices;
  Module: IOTAModule;
  SourceEditor: IOTASourceEditor;
  idx: integer;

begin
  Result := '';
  SourceEditor := nil;

  if SysUtils.Supports(BorlandIDEServices, IOTAModuleServices,
    ModuleServices) then
  begin
    Module := ModuleServices.CurrentModule;

    if System.Assigned(Module) then
    begin
      idx := Module.GetModuleFileCount - 1;

      // Iterate over modules till we find a source editor or list exhausted
      while (idx >= 0) and not SysUtils.Supports(Module.GetModuleFileEditor(idx), IOTASourceEditor, SourceEditor) do
        System.Dec(idx);

      // Success if list wasn't ehausted.
      if idx >= 0 then
        Result := ExtractFilePath(SourceEditor.FileName);
    end;

  end;

end;

procedure register;
begin
  RegisterComponents('Samples', [TTestLabel]);
  TestLabels.OnGetUnitPath := GetCurrentUnitPath;
end;

end.

关于delphi - 组件如何在设计时确定项目目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2433580/

相关文章:

multithreading - DELPHI:多线程客户端/服务器数据快照错误

delphi - 如何释放delphi应用程序中c++ dll中分配的内存

reactjs - 在 React 中提交表单后刷新页面

javascript - Ember - 绑定(bind)嵌套数组

delphi - Firebird 和 Delphi 2009 出现错误消息 "Incorrect values within SQLDA structure"

delphi - Delphi2009 的 PC-SC 包装器或 Omnikey Sync API 示例

delphi - 使用delphi 2010替换UTF-8文件中的unicode字符

delphi - Delphi的经纬度选择组件?

java - 在 Delphi 上使用 Wininet API 的 HTTP POST

delphi - 使用某些 RTL/VCL 或 Delphi 语言元素时如何生成警告