Finding the control you want


This page is a discussion of the various ways that a control can be located inside of a window, and how ProdUI addresses that.
At the root of it all, each control (or window) is identified by and OS assigned value called a _Handle_. Heres a nice discussion of it on MSDN.

Please note: for the purposes of this discussion, "window" will refer to the main application window, and "control" will be used to indicate one of the child windows that provide GUI interaction (a button, for example).

In the end, for ProdUI to be able to interact with the user interface, it needs the user to provide a way to get at that handle. One thing to remember is that since controls are assigned handles at runtime, hardcoding a handle into your program won't be reliable, since the value can (and usually will) change each time the window changes. That means we have to find a different way to get the handle.

  • By using the cursor location
  • By using a name or caption
  • By using a Control Id (or Resource Id)
  • By its position in the parent windows control tree

ProdUI control constructors  provide a uniform way to grab the desired control

  • Through passing in the handle obtained from another function
  • By passing in the parent window and the text that identifies the control. For example, the "7" button on the Calculator application that comes with windows can be addressed by using "7"

As stated above, there are other functions that can obtain a window or control handle, these are described fully in the user documentation included in the download. Relevant functions include:

  • GetControlHandle()
  • GetHandleFromTree()
  • GetWindowHandle() 

Ribbons

There's always an exception...

The major exception to the rule of having a handle or other unique Id are Ribbon controls and their constituent controls. These controls are dynamically drawn by the Windows Ribbon Framework and don't cantain an accesable window handle. ProdSpy can still take these apart and provide that controls index within the window control structure

 

Things to note:

  • With a window, ProdUI can use the text in the titlebar to grab a window handle
  • Controls, if designed using an environment that uses resource files (MFC), a control Id is available that can be used to uniquely identify a control. However, these are assigned by the resource compiler and may change from release to release
  • Each control occupies a node in what is called the UI Automation tree. The ultimate root of which is the desktop itself. By using the position of that node within the tree, we can navigate to that control and grab the handle. Without using methods included in the UI Automation framework, it is difficult to deduce a controls position. ProdSpy can be used to locate the index into the tree under the "Control Tree Position" line. Please remember, this number may change from release to release as well
  • Be mindful that automation investigation tools (ProdSpy included) may show the window handle value as the "Name" and/or "Automation Id" properties. This is not a flaw in the tools. The system has assigned these values to those properties, so another method should be used.
  • Some ComboBoxes will show the current selected item value as the "Name" or "Automation Id" properties. If this is the case, another method of obtaining the handle should be used.

Last edited Jun 8, 2011 at 1:44 AM by hroark, version 2

Comments

No comments yet.