VB6 Tutorial 64: The Screen Object

By | 2018-06-06

One of the objects you can work with in VB6 is the Screen object. This will allow you to find information about the current screen configuration, which will in turn allow you to position your forms and other output in the correct position on the users screen, regardless of the current resolution or dpi settings.

Object

You have come across many controls throughout this tutorial. All the controls i.e CommandButton, Label, TextBox are objects. Besides the form is also an object.

A Collection is an object. If you need a refresher on collections, check out the lesson about collections

Some more examples are Clipboard, Dictionary object, Drive and folder objects, App object, and printer object. There are many. The Screen object is also an object like them.

What is Screen Object?

The one line answer: The screen object corresponds to the visible desktop.

While working with the form’s properties, you have set Left, Top, Height and Width properties with a numeric value. Do you know the unit in which they are expressed? They are in twips.

On the printer, 1 inch = 1,440 twips.

Converting twips values into pixels

You can easily convert twips values into pixels using the TwipsPerPixelX and TwipsPerPixelY properties of the screen object.

Your computer screen’s resolution:

sWidth = Screen.Width / Screen.TwipsPerPixelX
sHeight = Screen.Height / Screen.TwipsPerPixelY
MsgBox "Resolution is " & sWidth & " X " & sHeight

This will get you the result in pixels.

Properties to know

Have a look at some more properties of the Screen Object.

MouseIcon

The MouseIcon property sets a custom mouse icon.

The MousePointer property

Changes the mouse pointer. But it applies to the current application only.

Example
Screen.MousePointer = vbCross

FontCount and Font properties

The FontCount property gives you the number of fonts for the current display device, and the Fonts property returns the font names.

The following program shows the font names.

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

The ActiveForm property

This property returns the form that is the active window.

The ActiveControl property

Returns the control that has the focus.

Text1.SetFocus
Screen.ActiveForm.Caption = "New Form"
Screen.ActiveControl.Text = "New text"

The ActiveForm and ActiveControl properties also only applies to the current application.

QuickHint:

All the properties except MouseIcon and MousePointer are read-only properties here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.