Recipe 10.17. Getting Display Dimensions


Problem

You want to determine the dimensions of the user's screen at runtime, including both the entire screen and just the working area that doesn't include the task bar. Also, you want to determine the number of monitors on the user's system, the screen dimensions of each, and which screen is currently active.

Solution

Sample code folder: Chapter 10\ScreenInfo

Access this information from the Screen object, which includes an array of objects, one for each screen on the system.

Discussion

The following code extracts information from each Screen object returned by the Screen.AllScreens property, then formats the various data items returned for easy review:

 Dim result As New System.Text.StringBuilder Dim scanScreen As Screen ' ----- Include some summary data. result.Append("Number of screens: ") result.AppendLine(Screen.AllScreens.Length.ToString) result.AppendLine( ) ' ----- Process each installed screen. For Each scanScreen In Screen.AllScreens    result.AppendLine("Device Name: " & _       GetTerminatedString(scanScreen.DeviceName))    result.AppendLine("Bounds: " & _       scanScreen.Bounds.ToString)    result.AppendLine("Working Area: " & _       scanScreen.WorkingArea.ToString)    result.AppendLine("Is Primary: " & _       scanScreen.Primary.ToString)    result.AppendLine( ) Next scanScreen MsgBox(result.ToString( )) 

The device name returned by the scanScreen.DeviceName property may include an old C-style terminating null character (ASCII 0), so you must to add a custom function to extract just the part you need:

 Private Function GetTerminatedString( _       ByVal sourceString As String) As String    ' ----- Return all text of a string up to the first    '       null character.    Dim index As Integer    index = sourceString.IndexOf(vbNullChar)    If (index > -1) Then       Return sourceString.Substring(0, index)    Else       Return sourceString End If     End Function 

As shown in Figure 10-25, the system used for testing this code had only one monitor, with a screen resolution of 1680 x 1050 pixels and a working area of 1680 x 990 pixels (the working area is slightly smaller because the task bar was showing along the bottom edge of the screen).

Figure 10-25. The Screen.AllScreens array provides information about any monitors on your system





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net