One of the most powerful features of the C++ Builder is its own Components that can be used with or without visuals. Components make programming easy; you can do many operations easily without knowing techniques like low coding or some specific functionality – you just let the component do the hard work for you. Every component that you drag into an app’s forms is an object of the TComponent class. For example, TEdit is a Class Type, a Component, and Edit1 is an Object from the TEdit Component.
RAD Studio runs in 32bits, which means all components should be 32bits. You can compile a 64bits VCL Component but you cannot install a 64bits VCL Component onto 32bits RAD Studio – only use them in your deployed 64bit apps. On the other side, you can create a 32bits VCL component that runs with your 64bits applications. That means while the RADS IDE is 32bits, you can create 64bits applications with your 64bits components. Now let’s see how we can do this.
In this post, you’ll get answers to these questions:
- How do I create a new VCL Component?
- How can I use the New Component menu?
- How can I use and fill in the New Component Wizard for the component applications?
- What is a Component in C++?
- What is an Object in C++?
By learning how to create a new windows VCL component in C++, it will help you to easily build C++ applications with the use of a C++ IDE.
What is a Component in C++ ?
In C++ Builder and Delphi, every object (i.e. Edit1) inherits from TObject class (i.e. TEdit1). Objects that can appear in the Form Designer inherit from TPersistent or TComponent Controls, which appear to the user at run time, inherit from TControl. There are two types of controls, graphic controls, which inherit from TGraphicControl, and windowed controls, which inherit from TWinControl. A control like TCheckBox inherits all the functionality of TObject, TPersistent, TComponent, TControl, and TWinControl, and adds specialized capabilities of its own.
A Component in C++ Builder and Delphi specifies the base class for all components. Components can be added to the Tool palette and manipulated at design time. Components can own other components. Com
The TComponent branch contains classes that descend from TComponent but not TControl. Objects in this branch are components that you can manipulate on forms at design time but which do not appear to the user at run time. They are persistent objects that can do the following:
- Appear on the Tool palette and be changed on the form.
- Own and manage other components.
- Load and save themselves.
Several methods introduced by TComponent dictate how components act during design time and what information gets saved with the component. Streaming (the saving and loading of form files, which store information about the property values of objects on a form) is introduced in this branch. Properties are persistent if they are published and published properties are automatically streamed.
The TComponent branch also introduces the concept of ownership, which is propagated throughout the component library. Two properties support ownership: Owner and Components. Every component has an Owner property that references another component as its owner. A component may own other components. In this case, all owned components are referenced in Components property of the component.
The constructor for every component takes a parameter that specifies the owner of the new component. If the passed-in owner exists, the new component is added to Components list of that owner. Aside from using the Components list to reference owned components, this property also provides for the automatic destruction of owned components. As long as the component has an owner, it will be destroyed when the owner is destroyed. For example, since TForm is a descendant of TComponent, all components owned by a form are destroyed and their memory freed when the form is destroyed. (Assuming, of course, that the components have properly designed destructors that clean them up correctly.)
If a property type is a TComponent or a descendant, the streaming system creates an instance of that type when reading it in. If a property type is TPersistent but not TComponent, the streaming system uses the existing instance available through the property and reads values for the properties of that instance.
Components that do not need a visual interface can be derived directly from TComponent. To make a tool such as a TTimer device, you can derive from TComponent. This type of component resides on the Tool Palette but performs internal functions that are accessed through code rather than appearing in the user interface at run time.
How to use the New Component Wizard
We can use the New Component wizard to create a new component for Delphi or C++Builder. The personality (Delphi or C++) of your current project is detected and used for the component.
- Create a folder for your component (i.e. on the Desktop create a “MyVCLComponent” folder)
- Select New Component… from Component menu of RAD Studio or C++ Builder as below
3. The New Component Wizard will help you to create your component. In the first window, to create a VCL component choose VCL for C++ Win32.
then Press the “Next>>” button.
4. Now we can choose an ancestor component. In this list, you will see that you can choose all official components and any installed components as an ancestor.
For a general control component you can choose TControl or, for example, if you want to create your own Edit (TEdit) box including its features you can choose TEdit. For a custom Button (TButton) you can choose Button etc.
In our example, we will make a custom animating Image component. So let’s search TImage from the search. Select TImage and then press the “Next>>” button.
5. In this step we should add a Class Name to our component. This Class Name should start, by convention, with an uppercase “T”. The T represents the Class Type. Let’s name our component as TMyVCLComponent. We can choice a category from the Palette Name or we can add a new Category. Let’s add a new category MyNewCategory. We should add A Unit name. The Unit file name should be different from the Class Name and it should be the full path to our component’s folder. In our example Unit Name can be “C:\Users\<UserName>\Desktop\MyVCLComponent\MyVCLCompo1.cpp“. The Wizard should look like this in accordance with your user name:
So In our example we used:
Class Name : TMyVCLComponent
Palette Name: MyNewCategory
Unit Name: <Folder Path\>”MyVCLCompo1.cpp” for example here “C:\Users\ata\Desktop\MyVCLComponent\MyVCLCompo1.cpp“
and then Press “Next>>” button.
6. We will create a new package so, select Install to New Package and then Press “Next>>” button.
7. Now we should add a full package name. Your project file should be in your MyVCLComponent Folder. Note that Package name shouldn’t be as same as the Class name. For Example it could be “C:\Users\ata\Desktop\MyVCLComponent\MyVCLCompo2021.cbproj” as below
8. Select the Finish Button.
9. Now it will ask where to save “MyVCLCompo1.cpp” file select our MyVCLComponent folder and keep file name same, and press Save button.
If you see a confirmation as below select Yes.
10. Now it should compile your first VCL component. When done you should see this message below.
11. After pressing OK you may see it is asking where to save its PCH file, select the same MyVCLComponent folder
12. Now you can Install your components by selecting Install from the right click menu over the “MyVCLCompo2021.bpl” file in your Project Group
For the any new changes you can check DocWiki about the Using the New Component Wizard
How do we test our new VCL component?
- Create a new C++ Builder Windows VCL Project
- On form design mode, in your palette you will se MyNewCategory you will see TMyVCLComponent. You can drag your new custom component to you form.
3. Now you can compile your project with your new component by pressing Run or F9. Note that you need your MyVCLCompo1.h – if your project asks, select the header file location or add it to a include path. You can select as below and press OK button.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.