Lazarus 0.9.30.4
我正在开发一个组件,当组件被放到表单上时,需要将几个单元添加到单元uses
子句中。
从this文章中我发现了设置设计器钩子并将所需文件添加到项目中的方法,可以将单元添加到uses
子句中。
代码示例:
procedure TTestComponent.DoCompAdded(APersistent: TPersistent; Select: boolean);
var
oProj: TLazProject;
oFile: TLazProjectFile;
begin
oProj := LazarusIDE.ActiveProject;
oFile := oProj.CreateProjectFile('X:\Unit path\AddonUnit.pas');
oFile.IsPartOfProject := True;
oProj.AddFile(oFile, True);
end;
constructor TTestComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
GlobalDesignHook.AddHandlerPersistentAdded(@DoCompAdded);
end;
procedure Register;
begin
RegisterComponents('AAA',[TTestComponent]);
end;
在调试时我确保调用了处理程序,单元AddonUnit
被添加到项目中(我通过oProj
对象内的所有文件进行了迭代)但是这导致了零结果:实际上没有任何内容添加到项目或uses
子句中。
此外,我发现项目文件集合oProj.Files[index]
返回当前打开的编辑器文件,而不是项目中包含的文件。
我是否遗漏了某些内容,或者是否有其他方式将所需单位包含在uses
条款或项目中?