C++ Builder Tutorials

C++ Builder: TApplication and TScreen

Each GUI application automatically declares a global Application variable, of the type TApplication.

There is also a global Screen variable, of type TScreen, with information about the current screen device.


TApplication

TApplication does not appear on the Component palette, nor is it available in the form designer to visually manipulate. Some properties of Application can be reached programmatically, or at design time in the pages "Application" and "Forms" of the Project / Options dialog box.

Some properties of the Application object:

ExeName is a read-only property that gives the name of the executable file. ExeName is the fully-qualified name, including the path to the application's executable.

Title contains the text that appears below the icon representing the application when it is minimized.

MainForm is the form that is the application's main window. The main form is the first form created in the application. When the main form closes, the application terminates.
When you create a new project, Form1 automatically becomes the value of the MainForm property. To assign a different form to the MainForm property, select the form on the Project / Options / Forms dialog box at design time. MainForm cannot be modified at run time.

Some methods of Application:

MessageBox(Text, Caption, Flags) displays a dialog box containing a message and one or more buttons. The parameters Caption and Flags are optional.
Parameters:
- Text is the message. Long messages are automatically wrapped in the message box.
- Caption appears in the title bar of the dialog box. A long caption results in a wide message box.
- Flags specifies what buttons appear in the message box. Possible values:
   MB_ABORTRETRYIGNORE: 3 buttons: Abort, Retry, and Ignore
   MB_OK: 1 button: OK; this is the default
   MB_OKCANCEL: 2 buttons: OK and Cancel
   MB_RETRYCANCEL: 2 buttons: Retry and Cancel
   MB_YESNO: 2 buttons: Yes and No
   MB_YESNOCANCEL: 3 push buttons: Yes, No, and Cancel
Returns one of the following values, depending on the button that was clicked:
   IDOK, IDCANCEL, IDABORT, IDRETRY, IDIGNORE, IDYES, IDNO

ProcessMessages interrupts the execution of an application, cycles the Windows message loop until it is empty, and then returns control to the application.
In lengthy operations, calling ProcessMessages periodically is important to allow the application to respond to paint, window resize and other messages. For example, if you try to resize the form while a lengthy routine is running, nothing would happen until that routine is finished.

Terminate ends the application execution. By calling Terminate, you shut down the application in an orderly fashion. In some cases Terminate is called automatically, such as when the main form closes.


TScreen

The global variable Screen lets you obtain information about the current state of the screen in an application.

Some properties of the Screen object:

Cursor controls the mouse cursor image on a global level. Cursor can be set to any of the cursor values available. This can be one of the built-in cursor values, or a custom cursor that has been added to the Cursors property. The global mouse cursor image remains in effect until the Cursor is changed back to crDefault.
A few built-in cursor values include:

  crDefault and crArrow: thick arrow pointing up and left
  crCross: fine cross-shaped cursor used in graphic applications for precise positioning
  crIBeam: text insert cursor in the form of a thin capital I
  crSizeNS: vertical resizing cursor
  crUpArrow: thin upward pointing cursor
  crHourGlass: hour glass cursor to indicate a busy process
  crDrag: drag cursor for single items being dragged
  crHelp: arrow pointing up and left with question mark
  crHandPoint: upward pointing hand cursor; used to identify a selectable item, such as a web page link

DesktopHeight gives the height of the desktop. The desktop is defined as the entire virtual desktop, which includes all monitors in the system. On a single-monitor system, DesktopHeight corresponds to Height.

DesktopWidth gives the width of the desktop. The desktop is defined as the entire virtual desktop, which includes all monitors in the system. On a single-monitor system, DesktopWidth corresponds to Width.

Fonts lists the names of the screen fonts that are currently installed. Applications can use Fonts to ensure that they do not request a font that is not installed on the user's system. When an application tries to use a font that is not installed, the system substitutes another font, which may not be appropriate to the application.

PixelsPerInch gives the number of screen pixels that make up a logical inch in the vertical direction.  PixelsPerInch is only accurate for vertical measurements. Most screens have a different scaling factor for horizontal measurements.


Code examples

Path and name of the application's .exe file

Label1.Caption = Application->ExeName;

Simple message box

ShowMessage("The file was not found", "Attention!");

Message box with a question

if (ShowMessage("Really delete it?", "Confirmation", MB_YESNO) == IDYES) {
  // delete the stuff
}

Screen Cursor and ProcessMessages

int i;
Screen.Cursor = crHourGlass;
for (i = 0; i < 50000; i++) {
  ListBox1->Items->Add("123456789");
  Application.ProcessMessages();
}
Screen.Cursor = crDefault;

Selecting a font
Place a TComboBox and a TMemo on a form. During form creation, a list of font names is loaded into the combo box. When the combo box is clicked, the memo's font is set to the corresponding font name in the combo box.

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  ComboBox1->Items = Screen->Fonts;
}

void __fastcall TForm1::ComboBox1Change(TObject *Sender) { Memo1->Font->Name = ComboBox1->Text; }