Recent

Author Topic: [SOLVED] How to get the name of a project in the code of a component ?  (Read 3634 times)

zacheus

  • New Member
  • *
  • Posts: 18
Hello,
Is it possible in the code of a new component to get the name of the project in which it is incorporated ? By example, I realize a component myComponent. I put it on Form1 of Project1. What code in myComponent could return 'project1' (in csDesigning or not) ?
Regards
 
« Last Edit: December 29, 2011, 09:00:09 pm by zacheus »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to get the name of a project in the code of a component ?
« Reply #1 on: December 29, 2011, 03:11:08 am »
I don't think it's possible. Only the IDE knows about 'project', the compiler doesn't. Here's a list of things the compiler know:
via {$I}: http://www.freepascal.org/docs-html/prog/progsu38.html
via macros: http://www.freepascal.org/docs-html/prog/progse5.html

zacheus

  • New Member
  • *
  • Posts: 18
Re: How to get the name of a project in the code of a component ?
« Reply #2 on: December 29, 2011, 08:56:08 am »
I found a solution in the http://wiki.lazarus.freepascal.org/Extending_the_IDE :

Quote
constructor TmyComponent.Create(AOwner: TComponent);
{uses  [...], LCLProc, FileUtil, LazIDEIntf, ProjectIntf;}
var
  i : integer;
  aLazProject : TLazProject;
begin
  inherited Create(AOwner);
 
  if (csDesigning in ComponentState) then begin 
   aLazProject := LazarusIDE.ActiveProject;
   if aLazProject<>nil then 
    showmessage(aLazProject.ProjectInfoFile); 
  end else
   showmessage(Application.ExeName);           
  [...]
end;

Showmessage in CsDesigning returns
  • project1.lpi if the project is not saved
  • ../../../nameoftheproject.lpi if the project is saved
Don't forget to add "IDEIntf" in the RequiredPkgs of the component.
Regards.
« Last Edit: December 30, 2011, 09:16:31 am by zacheus »

 

TinyPortal © 2005-2018