How can I create a new FMX Component? How can I use New Component menu? How can I use and fill the New Component Wizard for the FMX component applications? What is a Component in C++? What is an Object in C++? Let’s answer these questions.
One of the most powerful features of the C++ Builder is its own components that can be used with or without a visual representation. Components make programming easy, you can do many operations easily without knowing low-level coding. Every component that you drag into your forms is an object of that component class. For example
TEdit is a Class Type, a Component and Edit1 is an Object from the TEdit Component.
RAD Studio runs as a 32bit application. That means all design-time components should be 32bits too. You can compile 64bits components but you can NOT install a 64bits Component into 32bits RAD Studio as part of a design-time package. On the other side, you can create components that run with with your 64bit applications. This is why there are separate design time and runtime/build packages. When you choose 64bits as your compiler target and hit
compile” the 64bit build and runtime package code for the component is pulled in and used to create your application. That means even though the RAD Studio IDE is 32bits you can create 64bits applications with 64bits versions of components. Now let’s see how we can do this.
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.
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 do I use the “New Component” wizard in RAD Studio?
We can use New Component wizard to create a new component for Delphi or C++Builder. The personality of your current project is detected and used for the component.
- Create a folder for your component (i.e. on the Desktop create a “MyFMXComponent” folder)
- Select New Component… from Component menu of RAD Studio or C++ Builder as below
3. New Component Wizard will help you to create your component. In the first windows, to create a FMX component choice FireMonkey for C++
then Press “Next>>” button.
4. Now we can choice an ancestor component. In this list, you will see that you can choice all official components and installed components as an ancestor.
For a general control component you can choice TControl or for example if your want to create your own Edit (TEdit) box including its features you can choice TEdit. For a custon Button (TButton) you can choice 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 “Next>>” button.
5. In this step we should add Class Name to our component. This Class Name should start with T. T represents the Class Type. Let’s name our component as TMyFMXComponent. 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 full path in our component folder. In our example the Unit Name can be “C:\Users\<UserName>\Desktop\MyFMXComponent\MyFMXCompo1.cpp“. The wizard should look something like the following:
So in our example we used:
- Class Name : TMyFMXComponent
- Palette Name: MyNewCategory
- Unit Name: <Folder Path\>”MyFMXCompo1.cpp” for example here “C:\Users\ata\Desktop\MyFMXComponent\MyFMXCompo1.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 Full Package Name. Do not enter just a name here. Be sure that this full path is shown, otherwise your project can be created in default project folder. Our project file should be in our MyFMXComponent folder, thus press “…” and select our MyFMXComponent folder. Note that package name shouldn’t be as same as Class name. In out example let’s define our project as “MyFMXCompo2021“, so the project full path in our example will be something like “C:\Users\ata\Desktop\MyFMXComponent\MyFMXCompo2021.cbproj“. Consider that your user name is different so you must modify this in accordance with your user name or folder path.
8. Select the Finish Button.
9. Now it will ask where to save “MyFMXCompo1.cpp” file select our MyFMXComponent 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 FMX 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 MyFMXComponent folder as in example below
12. Now you can Install your components by selecting Install from the right click menu over the “MyFMXCompo2021.bpl” file in your Project Group
You can also Uninstall your component from this right click menu menu. If you have any problem or for the any new changes please check the Using the New Component Wizard DocWiki
Testing New FMX Component
- Create a new Multi-Device C++ Builder FMX Project
- On form design mode, in your palette you will se MyNewCategory you will see TMyFMXComponent. 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 MyFMXCompo1.h so 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.