1. Technology

TScreen Object Demystified


Delphi's TScreen Sample Application

TApplication, TScreen, and TForm are VCL classes that form the backbone of all Delphi applications by controlling the behavior of our project.
In this article, we are looking closely at TScreen object and its properties/methods.


Simply put, TScreen represents the state of the screen in which an application runs. This essential object tells us what Windows, and naturally Delphi, thinks are the properties of the entire screen. TScreen is used at runtime to keep track of forms and data modules that have been loaded as well as system specific information such as screen resolution and what fonts are available for display. For example, if we want to find out what Windows is assuming for the width of the screen, we should look at the Width property of the TScreen object.

Whenever we run a Delphi project, Delphi automatically creates the global Screen variable of type TScreen. Therefore, the TScreen object and its associated properties are available only at run time.

Common tasks performed by TScreen include specifying the look of the cursor, the size of the window in which your application is running, the list of fonts available to the screen device, the active form, and the active control within that form.

What follows are descriptions of the most common properties and events for the TScreen object. If you can't wait, download a sample project

Screen .Width, .Height

Height and Width read only properties tells us the height and width (resolution) of the screen, measured in pixels, as reported by Windows. The following code is used to center a form (poScreenCenter only works when the form is initially shown).
 Form1.Left := (Screen.Width - Form1.Width) div 2;
 Form1.Top := (Screen.Height - Form1.Height) div 2;

Screen .ActiveForm, .ActiveControl

This tells us what control (or which form) has the focus. We can read ActiveControl to learn which windowed control object in the active form currently receives the input from the keyboard.

Suppose you have two Edit (Edit1 and Edit2) boxes on a Form (Form1). Suppose you need to know what Edit box had the input focus (was selected) when you clicked a button (Button1). Here's How to find what control was "previously" selected

Screen .ComponentCount, .FormsCount, .DataModulesCount, .Forms

These properties are used when we need to, for example, iterate through the forms in an application or the components on a form.
 procedure TfrScreenObject.btnFormsClick(Sender: TObject) ;
   j: integer;
   //add the name of all forms on the screen to ListBox1
   for j := 0 to -1 + Screen.FormCount do
    ListBox1.Items.Add(Screen.Forms[j].Name) ;
 procedure TfrScreenObject.btnOhNoClick(Sender: TObject) ;
   j: integer;
   //change all TButton captions to 'Oh, no!'
   for j := 0 to -1 + Screen.ActiveForm.ComponentCount do
    if Screen.ActiveForm.Components[j] is TButton then
     (Screen.ActiveForm.Components[j] as TButton).Caption:='Oh, no!';

Screen .Fonts

This property gives us a list of the screen fonts (currently installed). The following code fragment assigns the font in ComoBox1 to Memo1 control Font property.
 // fill the combo with Font names

Screen .Cursors, .Cursor

Unlike the Cursor property for a component, the Cursor property for the Screen object has a global efect. It controls the cursor shape for all the forms in the application. (We can, of course, override this for an individual component through its Cursor property.) The Cursors property gives us a list of all the cursors available.

Assignments to the Screen object's cursor property are typically guarded by a try/finally statement to ensure that normal cursor behavior is restored. For example:

 Screen.Cursor := crHourglass;
 {Do some lengthy operation}
    Screen.Cursor := crDefault;
Cursor can be set to any of the cursor values available in the Cursors property. This can be one of the built-in cursor values, or a custom cursor (from resource file) that has been added to the Cursors property.
 procedure TForm1.Button1Click(Sender: TObject) ;
   MyCursor = 1;
   Screen.Cursors[MyCursor] := LoadCursorFromFile('c:\windows\cursors\globe.ani') ;
   Form1.Cursor := MyCursor;

Screen .OnActiveFormChange, .OnActiveControlChange Events

OnActiveFormChange occurs when the active form for the application changes (in a multi-form application). OnActiveControlChange occurs immediately after input focus changes to a new windowed control.

To use OnActiveFormChange event, define a procedure like this:

 TfrScreenObject = class(TForm)
 procedure ScreenOnActiveFormChange(Sender: TObject) ;
 procedure TfrScreenObject.ScreenOnActiveFormChange(Sender: TObject) ;
 //take same action upon Active Form change

and assign it in OnFormCreate (or any other convenient place) : Screen.OnActiveFormChange := ScreenOnActiveFormChange;

Solution to Coloring the Focused Data Entry Control

©2014 About.com. All rights reserved.