CBuilder VCL Forms Applications

C++Builder VCL Forms Applications

To create a C++Builder VCL Forms application, either double-click the VCL Forms Application item in the C++Builder Projects category on the Tool Palette or select File ® New ® VCL Forms Application — C++ for Win32.

Project Files

When you create a new C++Builder VCL Forms application project, the IDE creates the necessary project files and displays the project's main form on the Designer Surface. The IDE creates six files — three project files and three files for the main form.

The three project files are .bdsproj, .cpp, and .res. The .bdsproj file contains project settings, the .cpp file with the project name contains the essential code that makes the VCL Forms application work, and the .res file contains the icon for the project's executable file.

The three files created for the main form are .cpp, .h, and .dfm. When you start working on the form, the .cpp file will contain event handlers, the .h file will contain the form's interface (the list of components added to the form at design time and the list of event handlers), and the .dfm file will contain properties of the components added to the form. Listings 11-4A and 11-4B show the .cpp and .h files created for the main form.

The Source File

Listing 11-4A: The main form's .cpp file

image from book
//$$---- Form CPP ---- //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner) { } //---------------------------------------------------------------------------
image from book

The two #pragma directives after the #include directives are pretty important. The #pragma package directive ensures that units that reside in packages are properly initialized. All VCL components reside in packages.

The #pragma resource directive is used to link the .cpp, .h, and .dfm files together to make up a unit. The #pragma resource directive is the C++Builder equivalent of the {$R *.dfm} directive found in Delphi units. If you remove it, the .dfm will not be linked into the executable and the application won't be able to run.

Besides the #pragma directives and the Form1 variable, the .cpp file also contains the form's constructor:

__fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner) { }

In C++, the constructor is not called Create, as it is in Delphi, but instead has the same name as the class:

class MyClass { public:    MyClass(); // MyClass constructor };

The Header File

Listing 11-4B: The main form's .h file

image from book
//$$---- Form HDR ---- //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published:    // IDE-managed components private:         // User declarations public:         // User declarations    __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
image from book

The #ifndef and #define directives at the beginning of the header file are known as include guards. Include guards are put around the entire contents of the header file to make sure the contents of the file are only processed once at compile time, even if the header file is included in more than one unit in the project.

You should also consider protecting your own units with include guards. To protect your own unit, which in this example is called MyUnit.h, write this:

#ifndef MYUNIT_H #define MYUNIT_H // Contents of the MyUnit.h file #endif

The form's header file also includes several VCL units (SysUtils, Classes, StdCtrls, and Forms) and contains the form's interface. Every component you drop onto the form at design time will be added to the __published section:

class TForm1 : public TForm { __published:       // IDE-managed components    TButton *Button1;    TLabel *Label1; private:           // User declarations public:            // User declarations    __fastcall TForm1(TComponent* Owner); };

Building a Simple VCL Forms Application

To see how to build VCL Forms applications using the C++ language, let's first create a very simple application that displays "My First C++Builder VCL Forms Application" on the form's title bar, as shown in Figure 11-26.

image from book
Figure 11-26: A very simple C++Builder VCL Forms application

You can create this application either by modifying the form's Caption property in the Object Inspector or by writing some code in the form's constructor. The following listing shows several ways of accessing the form's Caption property in code. Note that the following code always uses the indirect member selector (-> operator). This operator must be used because all VCL components are stored on the heap.

Listing 11-5: Accessing component properties in code

image from book
__fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner) {    this->Caption = "My First ";    Form1->Caption = Form1->Caption + "C++Builder ";    Caption = Caption + "VCL Forms Application"; }
image from book

The C++Builder Reverse String Application

There are two ways you can create a C++Builder version of a Delphi application. You can either translate the code to C++ or you can add a Delphi form to your C++ project.

First, let's create the C++Builder Reverse String application by adding the already created Delphi form to a C++Builder project. To add a Delphi form to a C++Builder VCL Forms application, first create a new C++Builder VCL Forms application. Before saving the project to disk, you need to remove the automatically created main form by selecting Project ®Remove from Project. When the Remove From Project dialog box appears, select image from book Unit1.cpp in the list and click OK.

image from book
Figure 11-27: Removing the main form from the project

Now that you've removed the main form, you can use Project ® Add to Project to add an existing form to the project (see Figure 11-28). To see Delphi units in the dialog box, you'll probably need to select the Pascal unit filter in the Files of type drop-down list.

image from book
Figure 11-28: A multi-language VCL Forms project

When you add a Delphi form to a C++Builder project, Delphi will, at compile time, produce not only the .dcu file but will also generate a C++ header file for the Delphi form (with the .hpp extension) and produce an .obj file that can be used by the C++ compiler.

If you want, you can create a pure C++Builder VCL Forms application. The steps involved in creating a C++Builder and a Delphi VCL Forms application are the same. First you need to drop the necessary components on the Designer Surface (in this case you need two TEdit components and a TButton), then you have to double-click the button to have the IDE generate the event handler for the button's OnClick event:

void __fastcall TForm1::Button1Click(TObject *Sender) { }

Finally, you have to write the code to reverse the string:

void __fastcall TForm1::Button1Click(TObject *Sender) {    Edit2->Text = "";    for(int i=Edit1->Text.Length(); i > 0;  i--)       Edit2->Text = Edit2->Text + Edit1->Text[i]; }

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net