Creating Forms dynamically

Top  Previous  Next

DFM-Translator > Creating Forms dynamically

Unlike Delphi and C++Builder, C# compilers do not process DFM files. Forms and their components must therefore be created and initialized at runtime.

 

C++Builder applications can continue to use DFM files in the same way as Delphi applications. However, they can also create forms dynamically.

 

The intended behavior of Delphi2C# is to modify the translated project file when the conversion of DFM files is enabled.

 

The following example is based on GraphEx, one of the Embarcadero demo applications.

 

 

program GraphEx;

 

uses

  Forms,

  GraphWin in 'GraphWin.pas' {Form1},

  BMPDlg in 'BMPDlg.pas' {NewBMPForm},

  Vcl.Themes in 'Vcl.Themes.pas';

 

{$R *.RES}

 

begin

  Application.Initialize;

  Application.CreateForm(TForm1, Form1);

  Application.CreateForm(TNewBMPForm, NewBMPForm);

  Application.Run;

end.

 

A corresponding C++Builder project can register the forms and their DFM files with USEFORM:

 

using graphex.dfm                                                 

                                                                  

#include <vcl.h>                                                  

#pragma hdrstop                                                   

#include <tchar.h>                                                

//-----------------------------------------------------           

USEFORM("GraphWin.cpp", Form1);                                   

USEFORM("BMPDlg.cpp", NewBMPForm);                                

                                                                  

                                                                  

                                                                  

                                                                  

                                                                  

                                                                  

//-----------------------------------------------------           

int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)           

{                                                                 

       try                                                                   

       {                                                               

               Application->Initialize();                                    

               Application->MainFormOnTaskBar = true;                        

               Application->CreateForm(__classid(TForm1), &Form1);           

               Application->Run();                                           

       }                                                               

       catch (Exception &exception)                                    

       {                                                               

               Application->ShowException(&exception);                       

       }                                                               

       catch (...)                                                     

       {                                                               

               try                                                                  

               {                                                             

                       throw Exception("");                                              

               }                                                             

               catch (Exception &exception)                                  

               {                                                             

                       Application->ShowException(&exception);                     

               }                                                             

       }                                                               

       return 0;                                                       

}                                                                 

 

The USEFORM macros associate the generated C++ form classes with their DFM resources.                                                    

                                                                                                                     

 

Planned dynamic creation in C#   

 

Important: This translation is not yet implemented.

 

The following C# code is only a provisional example of the intended translation. The current version of Delphi2C# does not generate a complete, executable C# application from this project file.

 

In particular, the required implementation of TApplication is still missing. This includes the behavior of Initialize, CreateForm, Run, the global Application instance, and the management of the application's main form.

 

The example will therefore not compile or run without an additional compatibility implementation. It must not be regarded as currently supported Delphi2C# output.

 

The planned translation could have the following structure:                                                   

                                                                                        

using Bmpdlg;                                                                             

using Graphwin;                                                                           

using System;                                                                             

using Vcl.Forms;                                                                          

using Vcl.Themes;                                                                         

using static Bmpdlg.BmpdlgInterface;                                                      

using static Graphex.GraphexImplementation;                                               

using static Graphex.GraphexInterface;                                                    

using static Graphwin.GraphwinInterface;                                                  

using static System.SystemInterface;                                                      

using static Vcl.Forms.FormsInterface;                                                    

using static Vcl.Themes.ThemesInterface;                                                  

//---------------------------------------------------------------------------             

internal static class Program                                                             

{                                                                                         

    [STAThread]                                                                          

     static void Main()                                                                   

     {                                                                                    

         Application.Initialize();                                                        

         Application.CreateForm(TClass.Of<TForm1>(), new UntypedPointer(Form1));          

         Application.CreateForm(TClass.Of<TNewBMPForm>(), new UntypedPointer(NewBMPForm));

         Application.Run();                                                               

     }                                                                                    

 }                                                                                        

                                                                                                             

This example illustrates the intended structure only. The final generated code may change when TApplication and dynamic form creation are implemented.

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content