-1

I am attempting to do an unofficial integration with some already existing commercial Windows software. The goal is to detect when a certain textbox and/or label is visible on the screen via our service running on the machine, and then grab the text from said box/label and do something with it in the service. There is no external API we can leverage here.

My research has led me to see about intercepting Windows messages, but I have read a lot of conflicting information about how to go about this (I have never tried this before).

Is there a good way to accomplish my goal? Third party tools are acceptable, but the license must allow distribution to client sites.

Taylor Hill
  • 1,053
  • 1
  • 14
  • 24

1 Answers1

1

Taking into account that you just need to grab text from some control, you can use System.Windows.Automation classes.

You will have to find that textbox's corresponding AutomationElement by some property (AutomationIdProperty or ClassNameProperty).

var propCondition = new PropertyCondition(
    AutomationElement.AutomationIdProperty, "uniqueTextBoxId", PropertyConditionFlags.IgnoreCase);

return AutomationElement.RootElement.FindFirst(
    TreeScope.Element | TreeScope.Children, 
    propCondition);

AutomationIdProperty or ClassNameProperty can be determined, for example, with Inspect utility.

Then you can just get the required text from AutomationElement.

P.S.: WindowsAutomation is a bit heavyweight, so you should take care to not affect overall system performance with overly frequent polling for that element.

P.S.1 Sometimes neither AutomationIdProperty nor ClassNameProperty can be used to find the element. In that case you will have to use more complex conditions or even walk the tree manually.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53