Getting Tree View Item's Images

Applies to TestComplete 15.20, last modified on January 19, 2022

TreeView controls can display images next to items to indicate the item's state. A tree view item can have up to four images associated with it:

  • A default (unselected) image -- an image that is displayed when the item is not selected (for example, an open folder).
  • A selected image -- an image that is displayed when the item is selected (for example, a closed folder).
  • A state image -- this is an additional image displayed to the left of the default or selected image (for example, a checked, cleared or grayed check box). State images are used to indicate the application-defined item state.
  • An overlay image -- an image that is drawn transparently over the item’s default or selected image.

While testing tree view controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

The Win32TreeViewItem object, which is used to work with individual items in standard Win32 tree views, provide special properties that let you obtain and work with these images:

  • Image - Returns a Picture object that provides scripting access to the item’s default image.
  • SelectedImage - Returns a Picture object that corresponds to the item’s selected image.
  • StateImage - Returns a Picture object that corresponds to the item’s state image.

    In addition, you can use the StateImageIndex property to get the state image’s index (1-based) in the tree view’s state images list. 0 means that the tree view item does not have a state image.

  • OverlayImageIndex - Returns the 1-based index of the item’s overlay image in the images list. 0 means that the tree view item does not have an overlay image.

You can work with tree view items’ images in the same way as with ordinary images. For example, you can post them to the test log using the Log.Picture method, compare them with their baseline copies, and so on. For more information on available actions, see the Picture object description.

The example below demonstrates how you can obtain images associated with a tree view item. The LogItemImages routine checks which images the specified tree view has, and posts them to the test log.

JavaScript

function Main ()
{
  var p, w, folders, Item;

  // Open My Computer
  WshShell.Run("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p = Sys.Process("Explorer");
  w = p.Window("*WClass", "*");
  folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32");

  // Obtain the item that corresponds to the 1st folder on the C drive
  folders.ExpandItem("|Desktop|My Computer|*C:*");
  Item = folders.wItems.Item("Desktop").Items.Item("My Computer").Items.Item("*C:*").Items.Item(0);

  // Log information about the item's images
  LogItemImages (Item);
}

function LogItemImages (TreeViewItem)
{
  if (!strictEqual(TreeViewItem.Image, null))
    Log.Picture(TreeViewItem.Image, "Default image");

  if (!strictEqual(TreeViewItem.SelectedImage, null))
    Log.Picture(TreeViewItem.SelectedImage, "Selected image");

  if (!strictEqual(TreeViewItem.StateImage, null))
  {
    Log.Picture(TreeViewItem.StateImage, "State image");
    Log.Message("State image index (1-based): " + TreeViewItem.StateImageIndex);
  }

  if (TreeViewItem.OverlayImageIndex > 0)
    Log.Message("Overlay image index (1-based): " + TreeViewItem.OverlayImageIndex);
}

JScript

function Main ()
{
  var p, w, folders, Item;

  // Open My Computer
  WshShell.Run("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p = Sys.Process("Explorer");
  w = p.Window("*WClass", "*");
  folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32");

  // Obtain the item that corresponds to the 1st folder on the C drive
  folders.ExpandItem("|Desktop|My Computer|*C:*");
  Item = folders.wItems.Item("Desktop").Items.Item("My Computer").Items.Item("*C:*").Items.Item(0);

  // Log information about the item's images
  LogItemImages (Item);
}

function LogItemImages (TreeViewItem)
{
  if (TreeViewItem.Image != null)
    Log.Picture(TreeViewItem.Image, "Default image");

  if (TreeViewItem.SelectedImage != null)
    Log.Picture(TreeViewItem.SelectedImage, "Selected image");

  if (TreeViewItem.StateImage != null)
  {
    Log.Picture(TreeViewItem.StateImage, "State image");
    Log.Message("State image index (1-based): " + TreeViewItem.StateImageIndex);
  }

  if (TreeViewItem.OverlayImageIndex > 0)
    Log.Message("Overlay image index (1-based): " + TreeViewItem.OverlayImageIndex);
}

Python

def Main():

  # Open My Computer
  WshShell.Run("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL)

  # Obtain the Explorer process, window and the Folders tree
  p = Sys.Process("Explorer")
  w = p.Window("*WClass", "*")
  folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32")

  # Obtain the item that corresponds to the 1st folder on the C drive 
  folders.ExpandItem("|Desktop|My Computer|*C:*")
  Item = folders.wItems.Item("Desktop").Items.Item("My Computer").Items.Item("*C:*").Items.Item[0]

  # Log information about the item's images
  LogItemImages (Item)

def LogItemImages (TreeViewItem):
  
  if (TreeViewItem.Image != None):
    Log.Picture(TreeViewItem.Image, "Default image")

  if (TreeViewItem.SelectedImage != None):
    Log.Picture(TreeViewItem.SelectedImage, "Selected image")

  if (TreeViewItem.StateImage != None):
    Log.Picture(TreeViewItem.StateImage, "State image")
    Log.Message("State image index (1-based): " + TreeViewItem.StateImageIndex)

  if (TreeViewItem.OverlayImageIndex > 0):
    Log.Message("Overlay image index (1-based): " + TreeViewItem.OverlayImageIndex)

VBScript

Sub Main
  Dim p, w, folders, Item

  ' Open My Computer
  Call WshShell.Run("explorer.exe /e, /select,C:\", SW_SHOWNORMAL)

  ' Obtain the Explorer process, window and the Folders tree
  Set p = Sys.Process("Explorer")
  Set w = p.Window("*WClass", "*")
  Set folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32")

  ' Obtain the item that corresponds to the 1st folder on the C drive
  folders.ExpandItem("|Desktop|My Computer|*C:*")
  Set Item = folders.wItems.Item("Desktop").Items.Item("My Computer").Items.Item("*C:*").Items.Item(0)

  ' Log information about the item's images
  LogItemImages (Item)
End Sub

Sub LogItemImages (TreeViewItem)
  If Not (TreeViewItem.Image Is Nothing) Then
    Call Log.Picture(TreeViewItem.Image, "Default image")
  End If

  If Not (TreeViewItem.SelectedImage Is Nothing) Then
    Call Log.Picture(TreeViewItem.SelectedImage, "Selected image")
  End If

  If Not (TreeViewItem.StateImage Is Nothing) Then
    Call Log.Picture(TreeViewItem.StateImage, "State image")
    Log.Message("State image index (1-based): " & TreeViewItem.StateImageIndex)
  End If

  If TreeViewItem.OverlayImageIndex > 0 Then
    Log.Message("Overlay image index (1-based): " & TreeViewItem.OverlayImageIndex)
  End If
End Sub

DelphiScript

procedure LogItemImages (TreeViewItem);
begin
  if TreeViewItem.Image <> nil then
    Log.Picture(TreeViewItem.Image, 'Default image');

  if TreeViewItem.SelectedImage <> nil then
    Log.Picture(TreeViewItem.SelectedImage, 'Selected image');

  if TreeViewItem.StateImage <> nil then
  begin
    Log.Picture(TreeViewItem.StateImage, 'State image');
    Log.Message('State image index (1-based): ' + aqConvert.VarToStr(TreeViewItem.StateImageIndex));
  end;

  if TreeViewItem.OverlayImageIndex > 0 then
    Log.Message('Overlay image index (1-based): ' + aqConvert.VarToStr(TreeViewItem.OverlayImageIndex));
end;

procedure Main;
var p, w, folders, Item : OleVariant;
begin
  // Open My Computer
  WshShell.Run('explorer.exe /e, /select,C:\', SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p := Sys.Process('Explorer');
  w := p.Window('*WClass', '*');
  folders := w.Window('BaseBar').Window('ReBarWindow32').Window('SysTreeView32');

  // Obtain the item that corresponds to the 1st folder on the C drive
  folders.ExpandItem('|Desktop|My Computer|*C:*');
  Item := folders.wItems.Item['Desktop'].Items.Item['My Computer'].Items.Item['*C:*'].Items.Item[0];

  // Log information about the item's images
  LogItemImages(Item);
end;

C++Script, C#Script

function Main ()
{
  var p, w, folders, Item;

  // Open My Computer
  WshShell["Run"]("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p = Sys["Process"]("Explorer");
  w = p["Window"]("*WClass", "*");
  folders = w["Window"]("BaseBar")["Window"]("ReBarWindow32")["Window"]("SysTreeView32");

  // Obtain the item that corresponds to the 1st folder on the C drive
  folders["ExpandItem"]("|Desktop|My Computer|*C:*");
  Item = folders["wItems"]["Item"]("Desktop")["Items"]["Item"]("My Computer")["Items"]["Item"]("*C:*")["Items"]["Item"](0);

  // Log information about the item's images
  LogItemImages (Item);
}

function LogItemImages (TreeViewItem)
{
  if (TreeViewItem["Image"] != null)
    Log["Picture"](TreeViewItem["Image"], "Default image");

  if (TreeViewItem["SelectedImage"] != null)
    Log["Picture"](TreeViewItem["SelectedImage"], "Selected image");

  if (TreeViewItem["StateImage"] != null)
  {
    Log["Picture"](TreeViewItem["StateImage"], "State image");
    Log["Message"]("State image index (1-based): " + TreeViewItem["StateImageIndex"]);
  }

  if (TreeViewItem["OverlayImageIndex"] > 0)
    Log["Message"]("Overlay image index (1-based): " + TreeViewItem["OverlayImageIndex"]);
}

See Also

Working With Tree View Controls
Addressing Tree View Items
Checking Tree View Items' State
Image Property (TreeViewItem Objects)
OverlayImageIndex Property (TreeViewItem Objects)
SelectedImage Property (TreeViewItem Objects)
StateImage Property (TreeViewItem Objects)
StateImageIndex Property (TreeViewItem Objects)

Highlight search results