Getting List View Items' Images in Desktop Windows Applications

Applies to TestComplete 15.63, last modified on April 10, 2024

List view controls can display images next to items. This feature simplifies visual identification of individual items and allows the control to indicate their state. A list view item can have up to three images associated with it:

  • A full-sized icon that is displayed next to the item caption in the Icons and Tile modes.
  • A small icon that is displayed next to the item caption in the Small Icons, List and Report modes.
  • A state image - an additional image displayed to the left of the item’s ordinary image. Its purpose is to indicate the application-defined item state. Examples of state images are checked and cleared check boxes.

While testing list 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.

To let you work with list view items’ images, the Win32ListView object provides two special properties: the wImage property returns the item’s current image (either the large or small one), and the wStateImage property returns the item’s state image. Both properties return a Picture that provides a scripting interface to the corresponding image. If the specified item does not have an icon or a state image, the corresponding property returns an empty object (null in JavaScript, JScript, C#Script and C++Script, None in Python, Nothing in VBScript, nil in DelphiScript).

You can perform various operations over a list view item’s image, for example:

Below is a code snippet that demonstrates how you can work with the list view items’s images. The script obtains the item’s icon and posts it to the test log along with its width and height:

JavaScript

function Main()
{
  var p, ListView, img;

  p = Sys.Process("ListViewSample");
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32");

  img = ListView.wImage("Item 1");
  if (!strictEqual(img, null))
    Log.Picture(img, "The item's image.",
                "Image size: " + img.Size.Width + "x" + img.Size.Height)
  else
    Log.Message("The item has no image.");
}

JScript

function Main()
{
  var p, ListView, img;

  p = Sys.Process("ListViewSample");
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32");

  img = ListView.wImage("Item 1");
  if (img != null)
    Log.Picture(img, "The item's image.",
                "Image size: " + img.Size.Width + "x" + img.Size.Height)
  else
    Log.Message("The item has no image.");
}

Python

def Main():

  p = Sys.Process("ListViewSample")
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  img = ListView.wImage("Item 1")
  if img != None:
    Log.Picture(img, "The item's image.", \
                "Image size: " + img.Size.Width + "x" + img.Size.Height)
  else:
    Log.Message("The item has no image.")

VBScript

Sub Main
  Dim p, ListView, img

  Set p = Sys.Process("ListViewSample")
  Set ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  Set img = ListView.wImage("Item 1")
  If Not (img Is Nothing) Then
    Call Log.Picture(img, "The item's image.", "Image size: " & _
                     img.Size.Width & "x" & img.Size.Height)
  Else
    Log.Message("The item has no image.")
  End If
End Sub

DelphiScript

procedure Main;
var p, ListView, img : OleVariant;
begin
  p := Sys.Process('ListViewSample');
  ListView := p.Window('MyWndClass', 'Form1').Window('SysListView32');

  img := ListView.wImage['Item 1'];
  if img <> nil then
    Log.Picture(img, 'The item''s image.', 'Image size: ' +
                aqConvert.VarToStr(img.Size.Width) + 'x' + aqConvert.VarToStr(img.Size.Height))
  else
    Log.Message('The item has no image.');
end;

C++Script, C#Script

function Main()
{
  var p, ListView, img;

  p = Sys["Process"]("ListViewSample");
  ListView = p["Window"]("MyWndClass", "Form1")["Window"]("SysListView32");

  img = ListView["wImage"]("Item 1");
  if (img != null)
    Log["Picture"](img, "The item's image.",
                "Image size: " + img["Size"]["Width"] + "x" + img["Size"]["Height"])
  else
    Log["Message"]("The item has no image.");
}

See Also

Working With List View Controls in Desktop Windows Applications
wImage Property (Specific to Win32ListView Controls)
wStateImage Property (Specific to Win32ListView Controls)

Highlight search results