Screen Object in Visual Basic 6


Now after the previous lesson (MDI forms), lets move on to the next topic 'the Screen Object'. After coming to the 67th lesson, I think its not necessary to tell you about what an object is. Although I'll give you some hints.



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.

Collection is an object. See the lessons of collection object to clear any doubt.

Collection Object - part 1
Collection Object - Part 2   
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

Thus you get the result in pixels.





Some more properties of the Screen Object

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.

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.

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