UI Automation: How to change value of a horizontal

2019-08-09 02:45发布

问题:

I am trying to change the value of a horizontal scrollbar from -1 to -2. I am able to get access to it.. but next i have to change its value..

AutomationElement _sideBar = _ClickButtonElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "WindowsForms10.SCROLLBAR.app.0.378734a"));

_clickButtonElement is the AutomationElement of the parent window of the scrollbar.

回答1:

Scrollbars usually support RangeValuePattern. Use something like:

RangeValuePattern range = scrollbar.GetCurrentPattern(RangeValuePattern.Pattern) as RangeValuePattern;
range.SetValue(50); // Set to half way point

Note that usually scrollbars are normalized to 0..100, regardless of internal values. So if a scrollbar internally uses values -5 to 5, then the scrollbar's half-way point of 0 will actually be exposed via RangeValuePattern as 50.

You might want to use the Inspect tool to ensure that you are getting the correct element, and that it also supports this pattern. You can also use Inspect to call RangeValue.SetValue() through its UI before you write any code.



回答2:

AutomationElement aeForm = AutomationElement.FromHandle(windowPtr);

AutomationElementCollection buttonCollection = aeForm.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ScrollBar));          

AutomationElement aeButton = buttonCollection[1];

RangeValuePattern rcpattern = (RangeValuePattern)aeButton.GetCurrentPattern(RangeValuePattern.Pattern);
rcpattern.SetValue(50.00);