Width Property (Desktop Objects)

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Returns the desktop width, that is, the horizontal screen resolution in pixels.

Declaration

TestObj.Width

Read-Only Property Integer
TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section

Applies To

The property is applied to the following object:

View Mode

This property is available in the Object Browser panel and in other panels and dialogs in both Basic and Advanced view modes.

Property Value

The desktop width, in pixels.

Remarks

On systems with multiple monitors where desktop is extended across all displays, Sys.Desktop.Width returns the entire desktop’s width according to the display orientation specified in the Display Settings in the Control Panel. For example, if you have a dual 1280×1024 monitor setup, Sys.Desktop.Height and Sys.Desktop.Width report the following desktop size for different display orientations:

Display Orientation Sys.Desktop.Width Sys.Desktop.Height
Horizontal display orientation
2560
(1280 * 2)
1024
Vertical display orientation
1280 2048
(1024 * 2)

To get the screen width of individual monitors in a multi-monitor system with extended display, you can use one of the following approaches:

  • If all monitors have the same screen resolution:

    • If monitors are positioned horizontally side by side - each monitor’s screen width equals Sys.Desktop.Width divided by the number of monitors.

    • If monitors are positioned vertically side by side - each monitor’s screen width is the same as Sys.Desktop.Width.

  • If monitors have a different screen resolution, you can get their width and height by using external scripting objects and functions. For example, you can get this information from the WMI Win32_DesktopMonitor class or the .NET Framework Screen class. The following examples demonstrate these techniques:

    • Getting monitor screen resolution from WMI Win32_DesktopMonitor class.

      This script shows how you can query the WMI Win32_DesktopMonitor class to enumerate available monitors and get their screen resolution. For details on how to use WMI classes in tests, see Working With WMI Objects in Scripts.

      JavaScript

      function Win32_Test()
      {
        let objLocator = getActiveXObject("WbemScripting.SWbemLocator");
        let objWMIService = objLocator.ConnectServer(".", "root\\cimv2", "", "");
        let colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor");
        let enumerator = Utils.Enumerator(colItems);
        while (!enumerator.AtEnd)
        {
          Log.Message("Description: " + enumerator.Item.Description)
          Log.Message("Screen height: " + enumerator.Item.ScreenHeight)
          Log.Message("Screen width: " + enumerator.Item.ScreenWidth)
          enumerator.MoveNext();
        }
      }

      JScript

      function Win32_Test()
      {
        var objWMIService = GetObject("winmgmts:\\\\.\\root\\cimv2");
        var colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor");
        var enumItems = new Enumerator(colItems);

        var item;
        for (; !enumItems.atEnd(); enumItems.moveNext())
        {
          item = enumItems.item();
          Log.Message("Description: " + item.Description);
          Log.Message("Screen height: " + item.ScreenHeight);
          Log.Message("Screen width: " + item.ScreenWidth);
        }
      }

      Python

      def Win32_Test():
        objLocator = Sys.OleObject["WbemScripting.SWbemLocator"]
        objWMIService = objLocator.ConnectServer(".", "root\cimv2", "", "")
        colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor")
        
        enum = Utils.Enumerator(colItems)
        while not enum.AtEnd:
          Log.Message("Description: " + enum.Item.Description)
          Log.Message("Screen height: " + str(enum.Item.ScreenHeight))
          Log.Message("Screen width: " + str(enum.Item.ScreenWidth))
          enum.MoveNext()

      VBScript

      Sub Win32_Test
        Dim objWMIService, colItems, objItem

        Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
        Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor")
        For Each objItem In colItems
          Log.Message "Description: " & objItem.Description
          Log.Message "Screen height: " & objItem.ScreenHeight
          Log.Message "Screen width: " & objItem.ScreenWidth
        Next
      End Sub

      DelphiScript

      procedure Win32_Test;
      var objLocator, objWMIService, colItems, item, enum, i;
      begin
        objLocator := Sys.OleObject('WbemScripting.SWbemLocator');
        objWMIService := objLocator.ConnectServer('.', 'root\cimv2', '', '');
        colItems := objWMIService.ExecQuery('SELECT * FROM Win32_DesktopMonitor');

        enum := Utils.Enumerator(colItems);
        while not Enum.AtEnd do
        begin
          Log.Message('Description: ' + enum.item.Description);
          Log.Message('Screen height: ' + VarToStr(enum.item.ScreenHeight));
          Log.Message('Screen width: ' + VarToStr(enum.item.ScreenWidth));
          enum.MoveNext
        end;
      end;

      C++Script, C#Script

      function Win32_Test()
      {
        var objWMIService = GetObject("winmgmts:\\\\.\\root\\cimv2");
        var colItems = objWMIService["ExecQuery"]("SELECT * FROM Win32_DesktopMonitor");
        var enumItems = new Enumerator(colItems);

        var item;
        for (; !enumItems["atEnd"](); enumItems["moveNext"]())
        {
          item = enumItems["item"]();
          Log["Message"]("Description: " + item["Description"]);
          Log["Message"]("Screen height: " + item["ScreenHeight"]);
          Log["Message"]("Screen width: " + item["ScreenWidth"]);
        }
      }

    • Getting monitor screen resolution from .NET Screen class

      This script shows how you can use the .NET Screens.AllScreens property to enumerate available monitors and get their individual screen resolution. To run this example, add the System.Windows.Forms assembly to the CLR Bridge properties of your test project. See also Calling Functions From .NET Assemblies.

      JavaScript, JScript

      function NET_Test()
      {
        var screens, scr, i;

        screens = dotNET.System_Windows_Forms.Screen.AllScreens;
        for (i = 0; i < screens.Length; i++)
        {
          scr = screens.Get(i).Bounds;
          Log.Message("Screen width: " + scr.Width);
          Log.Message("Screen heigth: " + scr.Height);
        }
      }

      Python

      def NET_Test():
        screens = dotNET.System_Windows_Forms.Screen.AllScreens
        for i in range (0, screens.Length):
          scr = screens.Get(i).Bounds
          Log.Message("Screen width: " + str(scr.Width))
          Log.Message("Screen height: " + str(scr.Height))

      VBScript

      Sub NET_Test
        Dim screens, scr, i

        Set screens = dotNET.System_Windows_Forms.Screen.AllScreens
        For i = 0 To screens.Length - 1
          Set scr = screens.Get(i).Bounds
          Log.Message "Screen width: " & scr.Width
          Log.Message "Screen height: " & scr.Height
        Next
      End Sub

      DelphiScript

      procedure NET_Test;
      var screens, scr, i;
      begin
        screens := dotNET.System_Windows_Forms.Screen.AllScreens;
        for i := 0 to screens.Length - 1 do
        begin
          scr := screens.Get(i).Bounds;
          Log.Message('Screen width: ' + VarToStr(scr.Width));
          Log.Message('Screen height: ' + VarToStr(scr.Height))
        end
      end;

      C++Script, C#Script

      function NET_Test()
      {
        var screens, scr, i;

        screens = dotNET["System_Windows_Forms"]["Screen"]["AllScreens"];
        for (i = 0; i < screens["Length"]; i++)
        {
          scr = screens["Get"](i)["Bounds"];
          Log["Message"]("Screen width: " + scr["Width"]);
          Log["Message"]("Screen heigth: " + scr["Height"]);
        }
      }

See Also

Height Property (Desktop Objects)

Highlight search results