Getting the Trackbar Slider's Position in Desktop Windows Applications

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

When working with a trackbar control you can change numeric information by moving its slider. This numeric information corresponds to the slider's position. You may need to estimate the current slider's position or the available range to use them in your tests.

While testing trackbar 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 work with trackbar controls, TestComplete uses the Win32TrackBar object that provides a set of specific properties and methods used to work with trackbars. This object is associated with trackbar controls whose class names are listed in the Object Mapping list of the project’s options. The following properties of the Win32TrackBar object let you obtain information about the slider's position:

  • wMin Returns the minimum allowed slider position.
  • wPosition Returns the current position of the slider.
  • wMax Returns the maximum allowed slider position.
  • wSelectionStart Returns the position of the starting point of the selection range.
  • wSelectionEnd Returns the position of the end point of the selection range.

The following example demonstrates the usage of the above-mentioned properties. It obtains information about slider positions, increases the current slider position by 5 (if the new position is not out of range), and checks whether the tested trackbar control has a selection range.

JavaScript, JScript

function Main()
{
  var p, w, TrackBar, Slider, Length;

  // Obtain the trackbar object
  p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area");
  p.ClickItemR("Volume", false);
  p.PopupMenu.Click("Open Volume Control");
  w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume");
  TrackBar = w.Window("msctls_trackbar32", "", 4);

  // Post the slider's positions to the test log
  Log.Message("Minimum position:" + TrackBar.wMin)
  Log.Message("Maximum position:" + TrackBar.wMax)
  Log.Message("Current position:" + TrackBar.wPosition)

  // Increase the current slider position by 5
  Slider = TrackBar.wPosition + 5;
  if (Slider <= TrackBar.wMax)
  {
    TrackBar.wPosition = Slider;
    Log.Message("New position:" + TrackBar.wPosition);
  }
  else 
    Log.Message("The new position is out of range and cannot be adjusted");

  // Check whether the trackbar has a selection range
  Length = TrackBar.wSelectionEnd - TrackBar.wSelectionStart;
  if (Length != 0)
    Log.Message("The length of the selection range:" + Length)
  else 
    Log.Message("There is no selection range.");
}

Python

def Main():

  # Obtain the trackbar object
  p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
  p.ClickItemR("Volume", False)
  p.PopupMenu.Click("Open Volume Control")
  w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
  TrackBar = w.Window("msctls_trackbar32", "", 4)

  # Post the slider's positions to the test log
  Log.Message("Minimum position:" + TrackBar.wMin) 
  Log.Message("Maximum position:" + TrackBar.wMax)
  Log.Message("Current position:" + TrackBar.wPosition) 

  # Increase the current slider position by 5
  Slider = TrackBar.wPosition + 5
  if (Slider <= TrackBar.wMax):
    TrackBar.wPosition = Slider
    Log.Message("New position:" + TrackBar.wPosition)
  else:
    Log.Message("The new position is out of range and cannot be adjusted")

  # Check whether the trackbar has a selection range
  Length = TrackBar.wSelectionEnd - TrackBar.wSelectionStart
  if (Length != 0):
    Log.Message("The length of the selection range:" + Length)
  else:
    Log.Message("There is no selection range.")

VBScript

Sub Main
  Dim p, w, TrackBar, Slider, Length

  ' Obtain the trackbar object
  Set p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
  Call p.ClickItemR("Volume", False)
  Call p.PopupMenu.Click("Open Volume Control")
  Set w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
  Set TrackBar = w.Window("msctls_trackbar32", "", 4)

  ' Post the slider's positions to the test log
  Log.Message("Minimum position:" & TrackBar.wMin)
  Log.Message("Maximum position:" & TrackBar.wMax)
  Log.Message("Current position:" & TrackBar.wPosition)

  ' Increase the current slider position by 5
  Slider = TrackBar.wPosition + 5
  If Slider <= TrackBar.wMax Then
    TrackBar.wPosition = Slider
    Log.Message("New position:" & TrackBar.wPosition)
  Else 
    Log.Message("The new position is out of range and cannot be adjusted")
  End If

  ' Check whether the trackbar has a selection range
  Length = TrackBar.wSelectionEnd - TrackBar.wSelectionStart
  If Length <> 0 Then
    Log.Message("The length of the selection range:" & Length)
  Else
    Log.Message("There is no selection range.")
  End If
End Sub

DelphiScript

procedure Main();
var 
  p, w, TrackBar, Slider, Length: OleVariant;
begin
  // Obtain the trackbar object
  p := Sys.Process('Explorer').Window('Shell_TrayWnd').Window('TrayNotifyWnd').Window('SysPager').Window('ToolbarWindow32', 'Notification Area');
  p.ClickItemR('Volume', false);
  p.PopupMenu.Click('Open Volume Control');
  w := Sys.Process('SNDVOL32').Window('Volume Control', 'Master Volume');
  TrackBar := w.Window('msctls_trackbar32', '', 4);

  // Post the slider's positions to the test log
  Log.Message('Minimum position:' + aqConvert.VarToStr(TrackBar.wMin));
  Log.Message('Maximum position:' + aqConvert.VarToStr(TrackBar.wMax));
  Log.Message('Current position:' + aqConvert.VarToStr(TrackBar.wPosition));

  // Increase the current slider position by 5
  Slider := TrackBar.wPosition + 5;
  if (Slider <= TrackBar.wMax) then
    begin
      TrackBar.wPosition := Slider;
      Log.Message('New position:' + aqConvert.VarToStr(TrackBar.wPosition));
    end
  else 
    Log.Message('The new position is out of range and cannot be adjusted');

  // Check whether the trackbar has a selection range
  Length := TrackBar.wSelectionEnd - TrackBar.wSelectionStart;
  if Length <> 0
  then
    Log.Message('The length of the selection range:' + Length)
  else 
    Log.Message('There is no selection range.');
end;

C++Script, C#Script

function Main()
{
  var p, w, TrackBar, Slider, Length;

  // Obtain the trackbar object
  p = Sys["Process"]("Explorer")["Window"]("Shell_TrayWnd")["Window"]("TrayNotifyWnd")["Window"]("SysPager")["Window"]("ToolbarWindow32", "Notification Area");
  p["ClickItemR"]("Volume", false);
  p["PopupMenu"]["Click"]("Open Volume Control");
  w = Sys["Process"]("SNDVOL32")["Window"]("Volume Control", "Master Volume");
  TrackBar = w["Window"]("msctls_trackbar32", "", 4);

  // Post the slider's positions to the test log
  Log["Message"]("Minimum position:" + TrackBar.wMin);
  Log["Message"]("Maximum position:" + TrackBar.wMax);
  Log["Message"]("Current position:" + TrackBar.wPosition);

  // Increase the current slider position by 5
  Slider = TrackBar["wPosition"] + 5;
  if (Slider <= TrackBar["wMax"])
  {
    TrackBar["wPosition"] = Slider;
    Log["Message"]("New position:" + TrackBar["wPosition"]);
  }
  else 
    Log["Message"]("The new position is out of range and cannot be adjusted");

  // Check whether the trackbar has a selection range
  Length = TrackBar["wSelectionEnd"] - TrackBar["wSelectionStart"];
  if (Length != 0)
    Log["Message"]("The length of the selection range:" & Length)
  else 
    Log["Message"]("There is no selection range.");
}

See Also

Working With Trackbar Controls in Desktop Windows Applications
wMin Property (TrackBar and Slider Controls)
wMax Property (TrackBar and Slider Controls)
wPosition Property (TrackBar and Slider Controls)
wSelectionStart Property (TrackBar and Slider Controls)
wSelectionEnd Property (TrackBar and Slider Controls)

Highlight search results