Do you want to know about the available form methods you can use in C++ applications? How can we use mouse events on Forms? How can we get key presses made by users on our Forms? Let’s learn all about Methods of Forms on VCL Applications in C++ Builder.
C++ Builder is the easiest and fastest C and C++ IDE for building everything from simple to full-on 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.
Table of Contents
What are the basics of Forms (TForm) in the VCL Framework?
TForm represents a standard 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 visual objects, such as TButton, TCheckBox, TComboBox objects, and so on. All the Properties of Forms in the VCL 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->CloseModal(); Form1->Free(); // Free all the properties and components on this form from the memory |
How do I use Events of Forms (TForm) in VCL?
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 VCL
How we can get mouse clicks, and coordinates? Here is the answer, We can use FormMouseDown(), FormMouseMove(), FormMouseUp() 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 25 26 27 28 29 30 |
//--------------------------------------------------------------------------- void __fastcall TForm1::FormClick(TObject *Sender) { ShowMessage("Form Click"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDblClick(TObject *Sender) { ShowMessage("Double Click"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { ShowMessage("Mouse down at "+IntToStr(X)+","+IntToStr(Y) ); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { int x=X/10, y=Y/10; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { ShowMessage("Mouse up at "+IntToStr(X)+","+IntToStr(Y) ); } |
Using Keyboard Events of Forms (TForm) in VCL
How we can get key press on Forms ? The answer is, we can use FormKeyDown(), FormKeyUp() events as given example 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 |
//--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { switch (Key) { case vkReturn: ShowMessage("Enter pressed"); break; case vkEscape: Form1->Close(); break; case 'A': ShowMessage("A is pressed"); break; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift) { ShowMessage("Key Up"); } |
Using Other Events of Forms (TForm) in VCL
Here is a full VCL example that has most of events tested. To create these events, just double click to form events and modify each inside 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 |
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Form_Methods_VCL_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { ShowMessage("Closed"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose) { ShowMessage("Form Close Query"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { ShowMessage("Form Create"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { switch (Key) { case vkReturn: ShowMessage("Enter pressed"); break; case vkEscape: Form1->Close(); break; case 'A': ShowMessage("A is pressed"); break; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift) { ShowMessage("Key Up"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClick(TObject *Sender) { ShowMessage("Form Click"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDblClick(TObject *Sender) { ShowMessage("Double Click"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { ShowMessage("Mouse down at "+IntToStr(X)+","+IntToStr(Y) ); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { int x=X/10, y=Y/10; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { ShowMessage("Mouse up at "+IntToStr(X)+","+IntToStr(Y) ); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender) { ShowMessage("Painted"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormResize(TObject *Sender) { ShowMessage("Resized"); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormShow(TObject *Sender) { ShowMessage("Shown"); } //--------------------------------------------------------------------------- |
These methods show 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 VCL Forms can be found here and Methods of VCL Forms can be found here in official DocWiki.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition