开发者

Identifier for a Windows GUI element (AutomationElement HashCode and RuntimeID)

I am looking for a way to identify instances of a certain GUI element (for example the "copy" entry in the context menu of the Windows Explorer).

I tried both the .GetHashCode and the 开发者_StackOverflow中文版.GetRuntimeId method on the AutomationElement, but they both change each time I open the context menu.

What is the difference/purpose between RuntimeId and HashCode anyway?

Any ideas how I could identfy the GUI element without comparing the label/name string?


There are various ways, however under the AutomationElement class you are able to inspect the "Current" element and use a combination of ways to determine the element. The following example was taken from another solution See Here

using System.Window.Automation;        
private AutomationElement element;
System.Drawing.Point mouse = System.Windows.Forms.Cursor.Position;
this.element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y));

From here you are able to access the element.Current and extract things such as the Name, ProcessId.

Although this may be something you have already figured out, the following code should help you get the class name of the current UI item, however its NOT a part of the AutomationElement class and is a Win32 hook.

[DllImport("User32.dll")]
    public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

private string GetClassName(IntPtr hWnd)
    {
        StringBuilder sb = new StringBuilder(256);
        this.GetClassName(hWnd, sb, 256);
        return sb.ToString();
    }

Hope it helps, if only to get you started ;)

RH


Typically, you would have to navigate the GUI hierarchy. Starting at the AutomationElement representing the program (the main window), you would go down to the MenuBar (looping over all children), and then to the MenuItem you want (looping again). Each time you would have to e. g. check the current.Name property. Then you can read it, invoke it, use the ClickablePoint to find where to click it.. Also note that in order to get it, you also have to open the menu, so you would have to click there (or use some pattern) as well, first. Using SendKeys.Send if there is some easy Keyboard shortcut might be easier. As long as it is sufficient (could be alt + several cursor keys + enter as well).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜