In this post, you’ll learn about the available form methods, as well as how to use mouse events on forms. How can we capture and analyze key presses on forms? By learning form methods on FireMonkey Applications, it will help you to build C++ applications with the use of C++ software.
Table of Contents
C++ Builder makes writing C++ programs a lot easier
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. C++ Builder comes with Rapid Application Development Studio, also known as RAD Studio, and C++ Builder is one of the most professional IDE’s that work under RAD Studio. It is the oldest IDE (it began as Borland TurboC in 1990 and was later renamed Borland C++ Builder). Under the Embarcadero brand it comes with new versions, features, updates and support. 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.
You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter.
Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.
Forms (TForm) in FMX Framework and Its Methods
TForm represents a standard FireMonkey application window form. When we create forms in the Form designer at design time, they are implemented as descendants of TForm. Forms can represent the application’s main window, or dialog boxes, or various preferences-related windows. A form can contain any other FireMonkey objects, such as TButton, TCheckBox, TComboBox objects, and so on. All the Properties of Forms in the FireMonkey framework can be found here and Methods of Forms can be found here in official DocWiki.
We can use Hide() and Show() methods to hide and show our forms.
1 2 3 4 |
Form1->Hide(); Form1->Show(); |
We can use SendToBack() and BringToFront() methods, to send our form to the back or to bring to front.
1 2 3 4 |
Form1->SendToBack(); Form1->BringToFront(); |
We can use Deactivate() or Activate() methods to activate or deactivate event dispatcher.
1 2 3 4 |
Form1->Deactivate(); // Deactivate event dispatcher Form1->Activate(); // Activate event dispatcher |
We can use Close() or CloseModal() to close forms and we can also delete all form properties with Free() method.
1 2 3 4 5 6 |
Form1->Close(); //CloseForm //Form1->ClsoeModal(); Form1->Free(); // Free all the properties and components on this form from the memory |
Using Events of Forms (TForm) in FMX
One of the great feature of FireMonkey framework is events of components, these are methods that can be created by double clicking on the events of Object Inspector. Forms also has a lot of events.
To create and do something in a event of Form;
- Select Form
- Go to Object Inspector and Press Events Tab
- Double Click to value of event that you want to use, this will create this event in header file and method will be added in your cpp file automatically.
- Now you can add whatever you want to in that event
Here below screenshot example, we double click most of events of form and we added some codes in their procedures (methods).
Using Mouse Events of Forms (TForm) in FMX
For example we can use OnMouseDown(), OnMouseMove(), OnMouseUp() events to detect mouse button clicks with X and Y coordinates, and mouse wheel delta 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 |
//--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, float X, float Y) { ShowMessage("Mouse Down at "+FloatToStr(X)+","+FloatToStr(Y)); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, float X, float Y) { ShowMessage("Mouse Up at "+FloatToStr(X)+","+FloatToStr(Y)); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, float X, float Y) { float x=X/10, y=Y/10; // do something with x and y or X and Y } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta, bool &Handled) { ShowMessage("Mouse Wheel Delta is "+ IntToStr(WheelDelta)); } //--------------------------------------------------------------------------- |
Using Keyboard Events of Forms (TForm) in FMX
Here are the FormKeyDown() and FormKeyUp() keyboard examples 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 29 30 31 |
//--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar, TShiftState Shift) { switch (Key) { case vkReturn: ShowMessage("Enter pressed"); break; case vkEscape: Form1->Close(); break; } switch(KeyChar) { case 'A': ShowMessage("Uppercase A is pressed"); break; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, System::WideChar &KeyChar, TShiftState Shift) { ShowMessage("Key Up"); } //--------------------------------------------------------------------------- |
Using Other Events of Forms (TForm) in FMX
Here is a full FMX example that has most of events are tested 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
//--------------------------------------------------------------------------- #include <fmx.h> #pragma hdrstop #include "Form_Methods_FMX_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Form1->Hide(); Form1->Show(); Form1->Deactivate(); Form1->Activate(); Form1->SendToBack(); Form1->BringToFront(); Form1->BeginUpdate(); // Update all UI elements of on the form here Form1->EndUpdate(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar, TShiftState Shift) { switch (Key) { case vkReturn: ShowMessage("Enter pressed"); break; case vkEscape: Form1->Close(); break; } switch(KeyChar) { case 'A': ShowMessage("Uppercase A is pressed"); break; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, System::WideChar &KeyChar, TShiftState Shift) { ShowMessage("Key Up"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { ShowMessage("Created"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormShow(TObject *Sender) { ShowMessage("Shown"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { ShowMessage("Activated"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDeactivate(TObject *Sender) { ShowMessage("Deactivated"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormResize(TObject *Sender) { ShowMessage("Resized"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormHide(TObject *Sender) { ShowMessage("Hidden"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender, TCanvas *Canvas, const TRectF &ARect) { ShowMessage("Painted"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose) { ShowMessage("Closing"); CanClose =true; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { ShowMessage("Closed"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormTap(TObject *Sender, const TPointF &Point) { ShowMessage("Form Tapped"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { ShowMessage("Destroyed"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormFocusChanged(TObject *Sender) { ShowMessage("Focus Changed"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormVirtualKeyboardShown(TObject *Sender, bool KeyboardVisible, const TRect &Bounds) { ShowMessage("Virtual Keyboard Shown"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormVirtualKeyboardHidden(TObject *Sender, bool KeyboardVisible, const TRect &Bounds) { ShowMessage("Virtual Keyboard Hidden"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormGesture(TObject *Sender, const TGestureEventInfo &EventInfo, bool &Handled) { ShowMessage("Gesture Used"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, float X, float Y) { ShowMessage("Mouse Down at "+FloatToStr(X)+","+FloatToStr(Y)); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, float X, float Y) { ShowMessage("Mouse Up at "+FloatToStr(X)+","+FloatToStr(Y)); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, float X, float Y) { float x=X/10, y=Y/10; // do something with x and y or X and Y } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta, bool &Handled) { ShowMessage("Mouse Wheel Delta is "+ IntToStr(WheelDelta)); } //--------------------------------------------------------------------------- |
These methods shows how C++ Builder is capable to handle all of events of form windows. These methods may improve your applications, allows users to have better and faster user experience.
There are many methods and properties that we can use. All the Properties of Forms in FireMonkey frame work can be found here and Methods of Forms can be found here in official DocWiki.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition