Creating Forms dynamically |
Top Previous Next |
DFM-Translator > Creating Forms dynamically Compilers other than the C++Builder don't know DFM files and therefore forms have to be created dynamically. In Cpp-Builder projects form can be created by means of DFM files like in Delphi, But forms may be created dynamicall too. I fhte option to convert DFM files is enabled, the dpr file will be changed accordingly.
The two options are explained using the example of the GraphEx demo, which is one of the Embaradero example applications. graphex.dpr looks like:
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.
This becomes to:
using graphex.dfm dybnamic creation at runtime
#include <vcl.h> #include <vcl.h> #pragma hdrstop #pragma hdrstop #include <tchar.h> #include <tchar.h> //----------------------------------------------------- //----------------------------------------------------- USEFORM("GraphWin.cpp", Form1); #include "GraphWin.h" USEFORM("BMPDlg.cpp", NewBMPForm); #include "BMPDlg.h" //----------------------------------------------------- //----------------------------------------------------- int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { { try try { { Application->Initialize(); Application->Initialize(); Application->MainFormOnTaskBar = true; Form1 = new TForm1(Application, 0); Application->CreateForm(__classid(TForm1), &Form1); NewBMPForm = new TNewBMPForm(Application, 0); Application->Run(); Application->Run(); } Form1->ShowModal(); catch (Exception &exception) } { catch (Exception& exception) Application->ShowException(&exception); { } Application->ShowException(&exception); catch (...) } { catch (...) try { { try throw Exception(""); { } throw Exception(""); catch (Exception &exception) } { catch (Exception& exception) Application->ShowException(&exception); { } Application->ShowException(&exception); } } return 0; } } return 0; }
The differences are:
1. for pure C++ there will be no DFM file (otherwise components could be duplicated) 2. the pure C++ code doesn't need the USEFORM macro. 3. the pure C++ code doesn't use the Application->CreateForm method, but create the forms with new directly. 4. the pure C++ code needs an additional call of ShowModal for the main form of the application
The first form automatically created with Application.CreateForm becomes the main form of the application.
The call of Application->Run in the pure C++ code sometimes takes care that the code is cleaned correctly, when the application is closed.
It is very important, that forms have to be constructed like
Form1 = new TForm(this, 0); // in Delphi: CreateNew
with the second parameter being a dummy parameter that distinguishes it from the normally used constructor with one parameter only. In Vcl.Forms.pas two constructors for TForm are defined:
constructor TCustomForm.Create(AOwner: TComponent); constructor TCustomForm.CreateNew(AOwner: TComponent; Dummy: Integer = 0);
to which the constructors in Vcl.Forms.hpp correspond:
/* TCustomForm.Create */ inline __fastcall virtual TForm(System::Classes::TComponent* AOwner) : TCustomForm(AOwner) { } /* TCustomForm.CreateNew */ inline __fastcall virtual TForm(System::Classes::TComponent* AOwner, int Dummy) : TCustomForm(AOwner, Dummy) { }
When the first constructor is called, it always tries to read the DFM file, which then results in an error: EResNotFound exception will be thrown.
|
This page belongs to the Delphi2Cpp Documentation |
Delphi2Cpp home Content |