Screen class

The Screen class wraps the user’s primary display — its dimensions and twip-to-pixel ratio, the list of installed fonts, the currently active Form and the currently focused control on that form, and the application-wide mouse-pointer override. It is a singleton: there is exactly one Screen instance per process, owned by the runtime and exposed through the Screen property of the Global app-object. Code reaches it without qualification:

' Centre a form on the primary display
Me.Left = (Screen.Width  - Me.Width)  \ 2
Me.Top  = (Screen.Height - Me.Height) \ 2

' Show an hourglass cursor across the whole application during a long task
Screen.MousePointer = vbHourglass
LongRunningWork
Screen.MousePointer = vbDefault

Dimensions and DPI

Width and Height report the primary monitor’s dimensions in twips — the same unit forms and controls use by default. The conversion factors are exposed too:

On a 96-DPI display these are both 15 (1440 twips per logical inch ÷ 96 pixels per inch); on a 144-DPI display they are 10. Use them when interop with the Win32 API forces a conversion between pixels and the form-side coordinate system.

Note

twinBASIC’s Screen describes the primary monitor only. For per-monitor information in a multi-monitor configuration, fall through to the Win32 EnumDisplayMonitors / GetMonitorInfo API.

Active form and active control

ActiveForm returns the Form instance that is currently the foreground form in the application; ActiveControl returns the control within that form that currently holds the focus. Both return Nothing if no form in the application is active.

The most common idiom is reaching into the active form from a global handler — for example, a toolbar button on an MDIForm that operates on whatever MDI child is in front:

Private Sub tbrEdit_ButtonClick(ByVal Button As MSComctlLib.Button)
    Dim f As Form
    Set f = Screen.ActiveForm
    If f Is Nothing Then Exit Sub
    Select Case Button.Key
        Case "Cut":   f.ActiveControl.SelText = ""
        Case "Copy":  Clipboard.SetText f.ActiveControl.SelText
        ...
    End Select
End Sub

Fonts

FontCount is the number of fonts the OS reports for the current display context; Fonts(Index) returns the name of the font at Index0 to FontCount - 1. Together they let an application build a font-picker without going through the Win32 EnumFontFamilies API.

Dim i As Integer
For i = 0 To Screen.FontCount - 1
    cboFonts.AddItem Screen.Fonts(i)
Next

Mouse pointer override

MousePointer is an application-wide cursor override. Setting it to anything other than vbDefault forces the chosen cursor over every window of the application, regardless of each individual control’s own MousePointer setting — the typical use is showing the hourglass while a synchronous operation runs. Set it back to vbDefault when the operation completes.

MouseIcon supplies a custom StdPicture to use when MousePointer is vbCustom.

Properties

ActiveControl

The control on the ActiveForm that currently has the input focus, as a Control reference, or Nothing if no form is active. Read-only.

ActiveForm

The Form that is currently the foreground form in the application, or Nothing if no form is active. Read-only.

FontCount

The number of fonts the OS reports as available on the current display context. Integer, read-only.

Fonts

The name of the font at the given zero-based index, in the order the OS reported it. String, read-only.

Syntax: object.Fonts( Index )

Index
required An Integer in the range 0 to FontCount - 1. Out-of-range indices return an empty string.

Height

The height of the primary display, in twips. Single, read-only.

MouseIcon

The custom cursor picture used when MousePointer is vbCustom, as a StdPicture. Readable, writable (Let), and assignable by reference (Set).

MousePointer

The application-wide mouse-pointer override, as a member of MousePointerConstants. Integer, readable and writable.

Setting MousePointer to anything other than vbDefault (0) forces the chosen cursor over every window of the application, ignoring per-control overrides. The typical use is showing vbHourglass during a synchronous long-running operation; set it back to vbDefault when the operation finishes.

TwipsPerPixelX

The number of twips per horizontal pixel on the primary display, as a Single. Effectively 1440 / dpi_x. Returned by a parameterless function call.

Syntax: object.TwipsPerPixelX( )

TwipsPerPixelY

The number of twips per vertical pixel on the primary display, as a Single. Effectively 1440 / dpi_y. Returned by a parameterless function call.

Syntax: object.TwipsPerPixelY( )

Width

The width of the primary display, in twips. Single, read-only.