How do you feel about IFDEF in the use section of .dpr?

Whenever you add a new module to a project, Delphi restores the .dpr file and all the IFDEFs in the uses section are gone.

To get around this, I usually use NotePad to create new .pas files and add them to the .dpr manually. If I need a form, I use File-> New-> Form, and then revert the .dpr file to the previous version. Not very happy if you ask me; -)

How do you deal with this? Is there a way to add a block to the IDE while maintaining the IFDEF?

+5
source share
7 answers

IFDEF , IDE , dpr. dpr uses. , .

+9

, ,

(.dpr) , | | / ,

, .dpr, , .

, , . / .dpr.

, , , , .dpr , click/cmd

+3

ifdefs dpr. / , - , .

+3

IDE. ( " " ).

dpr "". . , , , .

+2

, datamodules , , , . , ( , ).

1) , , , ( , ) [, uSpecialParent.pas]

2) , , . , , , , :

TYPE
  TMySpecialFormClass : class of TForm;

VAR
  TMySpecialForm : TMySpecialFormClass;

3) , IFDEFS. :

Unit uRegisterSpecialForms;

interface

uses
{$IFDF SPECIAL1}
  uSpecial1,
{$ENDIF}
{$IFDEF SPECIAL2}
  uSpecial2,
{$ENDIF}
  uSpecialParent;

implementation

// no code needed.

initialization

{$IFDEF SPECIAL1}
  TMySpecialForm := uSpecial1.TSpecialForm1;
{$ENDIF}
{$IFDEF SPECIAL2}
  TMySpecialForm := uSpecial2.TSPecialForm2;
{$ENDIF}

end.

4) , uSpecialParent , , , , , :

var
  frm : TForm;
begin
  frm := TMySpecialForm.Create(nil);
  try
    frm.showmodal;
  finally
    frm.free;
  end;
end;
+1

lo-tech :

, IDE :

  • DPR , WinMerge
  • IDE
  • DPR
+1

(Delphi 7)

. :

program Project1;

{$IFDEF TESTIFDEF}
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$ELSE}
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};
{$ENDIF TESTIFDEF}

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

, (Unit2.pas) IFDEF, "TESTIFDEF", , ( {$ ELSE}).

, :

  • IFDEF "{$ IFDEF DELPHIBASISCONFIGURATION}" "{$ IFDEF TESTIFDEF}", .
  • LABELS , .
  • , , - ...
  • define
  • "DELPHIBASISCONFIGURATION";)

Therefore, it should look like this:

program Project1;

{$DEFINE MYCONFIG1} // THIS ONE IS NOW ACTIVE


{$IFDEF DELPHIBASISCONFIGURATION}
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3};

{$ELSE}
     // THIS IS A "COMMON TO ALL CONFIG" PART
     uses
       Forms,

       // FIRST CONFIGURATION
       {$IFDEF MYCONFIG1}
       Unit1 in 'Unit1.pas' {Form1},
       Unit3 in 'Unit3.pas' {Form3}
       {$ENDIF MYCONFIG1}

       // SECOND CONFIGURATION    
       {$IFDEF MYCONFIG2}
       Unit1 in 'Unit1.pas' {Form1},
       Unit2 in 'Unit2.pas' {Form2}
       {$ENDIF MYCONFIG2}

     // THIS IS THE "COMMON TO ALL CONFIG" END :)
     ;

{$ENDIF TESTIFDEF}

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  //Application.CreateForm(TForm3, Form3);
  //Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

As you can see, I canceled the Application.CreateForm (...) calls for Form2 and Form3.

IMHO, it is usually better to dynamically create additional forms at the moment when you really need them, i.e. not all forms at program start ...

0
source

All Articles