Five Simple Examples Of C++ FMX Applications
The C++ Builder CE Community Edition is a free version of professional C++ Builder that you can develop GUI based desktop and mobile applications in C++. In this post, we will give you five simple C++ FMX applications as examples that you can compile with C++ Builder 11 CE.
The latest C++ Builder 11 CE was released in April 2023. If you are a start-up developer, student, hobbyist or just interested in learning to code then C++ Builder Community Edition may well be just the thing for you. Read the FAQ notes on the CE license and then simply fill out the form and download C++ Builder 11 CE.
If you download C++ Builder Community Edition (or RAD Studio CE version) or any Professional, Architect, Enterprise versions of C++ Builder. Install it on your windows computer and run RAD Studio or C++ Builder. Beginners and students normally start to learn C++ with simple code. Let’s create a new Multi-Device C++ application for Windows by using FMX framework in C++ Builder CE.
How to develop C++ FMX applications in C++Builder CE?
If you download and install the C++ Builder CE Community Edition then we can start to coding,
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. VCL applications focus only Windows, if you want to develop same app for multiple-OS’es you can use FMX. FMX and VCL mostly has similar components and component properties and methods, there are some small changes in different components. There are also significant changes in graphics to support multi-OS devices. Personally, I found that FMX has more options in graphics and much more powerful than VCL in graphical operations. Let’s start to develop a C++ app with GUI using FMX framework.
1. Choose File->New-> “Multi-Device Application – C++ Builder” menu.
This will create a New C++ Project for Windows. This will allow you develop C++ apps with FMX UI elements. If you don’t need UI Elements, this means you don’t need VCL or FMX frameworks, you create a console application too. Modern applications have a GUI’s and skinned Styles. Note that VCL projects are Windows only and FireMonkey projects are Multi Device (multi-platform) applications that you can compile and run on Windows, MacOS, iOS and Android.
2. Save all Unit files and Project file to a folder.
3. Drag components to your form design
Simply drag and drop components from the Palette window on the right side; Memo (TMemo) and Button (TButton) to your form design. Arrange their width, height and position. You can edit each of their properties from the Object Inspector on the left side.
Note that you can switch between the GUI Design mode to Code Mode by pressing F12, or vice versa. If you want, you can switch to your header file (.h) of your cpp file (.cpp) from the button tabs. You can change your Build Configuration from the left Project window by setting it to Debug or Release mode.
//—————————————————————————
#include #pragma hdrstop
#include “Unit1.h” //————————————————————————— #pragma package(smart_init) #pragma resource “*.dfm” TForm1 *Form1; //————————————————————————— __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } |
4. Double click to Button1 to create OnClick() event for this button. Add these lines into Button1Click() event, between { and } brackets as given below,
void __fastcall TForm1::Button1Click(TObject *Sender) { String str = “Hello World”;
Memo1->Lines->Add(str); } |
This example above is a modern “Hello World” example for Windows which runs with C++ Builder.
5. Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together. You can also use the Run menu from the top to run your application. You should see this below as a form application
If you get an error, check your code – it’s easy to mistype or have the braces (“}” characters) in the wrong place. Note that C++ is case sensitive, so upper and lower characters should be also same. Then press the F9 key or just click the Run button again.
You can change the font property of Memo, here we used Consolas 9 font that has fixed width.
From this point, you can add more codes or you can switch back to design mode by pressing F12. And you can add more components, then more code. This is how modern C++ coding works with an UI Toolkit.
How to do calculations in C++ FMX applications?
As in same example above, now lets do another simple calculation. We have a price of a product and there is VAT percentage, then we want to calculate total price of this product. To do this, we should set price and VAT, then we can calculate VATprice and at the final we need to print price and VATprice as a total to the Memo as below,
void __fastcall TForm1::Button1Click(TObject *Sender) { float price = 100; float VAT = 10; float VATprice = price*VAT/100;
Memo1->Lines->Add( “TotalPrice = “ + FloatToStr(price+VATprice) ); } |
Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together.
How to enhance C++ FMX applications with C++Builder CE?
As in the same example above, now lets do a simple calculation. We have a price of a product and there is VAT percentage, then we want to calculate total price of this product. To do this, we should set price and VAT, and we need to calculate VATprice and totalprice too. So we can define these variables below,
float price = 100; // price of a product float VAT = 10.0; // percentage of VAT 10% float VATprice, totalprice;
|
We can define a UnicodeString
to set formated texts, then we can print this. UnicodeString has a printf function in modern C++ as in classic C function. Thus, we can print inputs and calculate totalprice as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void __fastcall TForm1::Button1Click(TObject *Sender) { float price = 100; // price of a product float VAT = 10.0; // percentage of VAT 10% float VATprice, totalprice;
UnicodeString ustr;
Memo1->Lines->Add(“Inputs:”); ustr.printf(L“Price = $%8.2f”, price); Memo1->Lines->Add(ustr);
ustr.printf(L“VAT = %8.2f%”, VAT); Memo1->Lines->Add(ustr);
Memo1->Lines->Add(“”); Memo1->Lines->Add(“Calculation:”); VATprice = price*VAT/100; // VAT price addition
ustr.printf(L“VAT Price = $%8.2f”, VATprice); Memo1->Lines->Add(ustr);
totalprice= price+VATprice; ustr.printf(L“Total Price = $%8.2f”, totalprice); Memo1->Lines->Add(ustr); }
|
Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together. You can also use the Run menu from the top to run your application. You should see this below as a form application.
How to get input in C++ FMX applications?
Lets continue to same example. Let’s add more components to get inputs from user. Todo this, drag 2 Labels (TLabel) and 2 Edits (TEdit) components, and we will have a Memo (TMemo) and a Button (TButton) as below,
Double click to Button1, first we need to define our variables. Then we can get the price and VAT values from the Text of Edit1 and Edit2. We should convert Text to float values by using StrToFloat() function as below,
float price, VAT, VATprice, totalprice;
price = StrToFloat(Edit1->Text); VAT = StrToFloat(Edit2->Text);
|
and other calculations and printing will be same as examples above. Thus, all Button1Click() event will be as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void __fastcall TForm1::Button1Click(TObject *Sender) { float price = 100; // price of a product float VAT = 10.0; // percentage of VAT 10% float VATprice, totalprice;
UnicodeString ustr;
Memo1->Lines->Add(“Inputs:”); ustr.printf(L“Price = $%8.2f”, price); Memo1->Lines->Add(ustr);
ustr.printf(L“VAT = %8.2f%”, VAT); Memo1->Lines->Add(ustr);
Memo1->Lines->Add(“”); Memo1->Lines->Add(“Calculation:”); VATprice = price*VAT/100; // VAT price addition
ustr.printf(L“VAT Price = $%8.2f”, VATprice); Memo1->Lines->Add(ustr);
totalprice= price+VATprice; ustr.printf(L“Total Price = $%8.2f”, totalprice); Memo1->Lines->Add(ustr); }
|
Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together. You can also use the Run menu from the top to run your application. This time our application is able to calculate different values. In example, you can see this below as a form application.
Example 5. How to use loops in C++Builder CE FMX application?
We can create another example to calculate the price of many different product counts. Here is how you can use for() loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
void __fastcall TForm1::Button1Click(TObject *Sender) { float price, VAT, VATprice, totalprice;
price = StrToFloat(Edit1->Text); VAT = StrToFloat(Edit2->Text);
UnicodeString ustr;
for (int i = 0; i < 1000; i++) { ustr.printf(L“count=%4d Price = $%6.2f Total Price=$%6.2f”, i, price, i*price*(1+VAT/100)); Memo1->Lines->Add(ustr); } }
|
Now you can compile this C++ code; just press the F9 key or just click the Run button in the center of top bar. This will let the IDE check your code and compile both the Unit and project files and link them together. You can also use the Run menu from the top to run your application. This time our application is able to calculate different values of counts in a loop. You should see this below as a form application.
Note that, If you want to see results faster, you can use Memo1->Lines->BeginUpdate();
before the for()
loop, and Memo1->Lines->EndUpdate();
after the for() { }
loop;
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 version.