在Lazarus下,在表单上添加组件时注册所需的单元失败

时间:2012-04-27 07:27:19

标签: lazarus fpc

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条款或项目中?

0 个答案:

没有答案