使用Inspect.exe,我可以看到应用程序组件的树结构中存在一个按钮,但是我找不到找到该按钮的句柄的方法。这是控件的Inspect.exe输出:
How found: Selected from tree...
Name: "Options"
ControlType: UIA_ButtonControlTypeId (0xC350)
LocalizedControlType: "Button"
BoundingRectangle: {l:805 t:286 r:821 b:302}
IsEnabled: true
IsOffscreen: false
IsKeyboardFocusable: true
HasKeyboardFocus: false
ProcessId: 4380
RuntimeId: [2A.103FA.2.F6EBAC8.0]
AutomationId: ""
ClassName: "NetUISimpleButton"
IsControlElement: true
IsContentElement: false
ProviderDescription: "[pid:4380,hwnd:0x0 Main(parent link):Unidentified Provider (unmanaged:mso.dll)]"
IsPeripheral: [Not supported]
LiveSettingProperty: [Not supported]
HelpText: "Options"
FlowsFrom: [Not supported]
OptimizeForVisualContent: [Not supported]
Annotation.AnnotationAuthor: [Not supported]
Annotation.AnnotationTypeId: [Not supported]
Annotation.Author: [Not supported]
Annotation.DateTime: [Not supported]
Annotation.Target: [Not supported]
Drag.DropEffect: [Not supported]
Drag.DropEffects: [Not supported]
Drag.GrabbedItems: [Not supported]
Drag.IsGrabbed: [Not supported]
DropTarget.DropTargetEffect: [Not supported]
DropTarget.DropTargetEffects: [Not supported]
LegacyIAccessible.ChildId: 0
LegacyIAccessible.DefaultAction: "Press"
LegacyIAccessible.Description: ""
LegacyIAccessible.Help: "Options"
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: "Options"
LegacyIAccessible.Role: push button (0x2B)
LegacyIAccessible.State: focusable (0x100000)
LegacyIAccessible.Value: ""
ObjectModel.UnderlyingObjectModel: [Error: calling getter for this property: hr=0xFFFFFFFF80070057 - The parameter is incorrect.]
SpreadsheetItem.AnnotationObjects: [Not supported]
SpreadsheetItem.AnnotationTypes: [Not supported]
SpreadsheetItem.Formula: [Not supported]
Style.ExtendedProperties: [Not supported]
Style.FillColor: [Not supported]
Style.FillPatternColor: [Not supported]
Style.FillPatternStyle: [Not supported]
Style.Shape: [Not supported]
Style.StyleId: [Not supported]
Style.StyleName: [Not supported]
Transform2.CanZoom: [Not supported]
Transform2.ZoomLevel: [Not supported]
Transform2.ZoomMinimum: [Not supported]
Transform2.ZoomMaximum: [Not supported]
IsAnnotationPatternAvailable: [Not supported]
IsDragPatternAvailable: [Not supported]
IsDockPatternAvailable: false
IsDropTargetPatternAvailable: [Not supported]
IsExpandCollapsePatternAvailable: false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable: true
IsItemContainerPatternAvailable: false
IsLegacyIAccessiblePatternAvailable: true
IsMultipleViewPatternAvailable: false
IsObjectModelPatternAvailable: [Not supported]
IsRangeValuePatternAvailable: false
IsScrollItemPatternAvailable: true
IsScrollPatternAvailable: false
IsSelectionItemPatternAvailable: false
IsSelectionPatternAvailable: false
IsSpreadsheetItemPatternAvailable: [Not supported]
IsSpreadsheetPatternAvailable: [Not supported]
IsStylesPatternAvailable: [Not supported]
IsSynchronizedInputPatternAvailable: false
IsTableItemPatternAvailable: false
IsTablePatternAvailable: false
IsTextChildPatternAvailable: [Not supported]
IsTextEditPatternAvailable: [Not supported]
IsTextPatternAvailable: false
IsTextPattern2Available: [Not supported]
IsTogglePatternAvailable: false
IsTransformPatternAvailable: false
IsTransform2PatternAvailable: [Not supported]
IsValuePatternAvailable: false
IsVirtualizedItemPatternAvailable: false
IsWindowPatternAvailable: false
FirstChild: "" Image
LastChild: "" Image
Next: "Show Menu" Button
Previous: [null]
Other Props: Object has no additional properties
Children: "" Image
Ancestors: "" SplitButton
"Contacts" Pane
"" Pane
"" Custom Control
"" Pane
"" pane
"Lync" window
"Desktop" pane
[ No Parent ]
Run Code Online (Sandbox Code Playgroud)
关于此按钮的奇特之处在于它没有hwnd值。(hwnd:0x0)。这是我试图获得对该按钮的引用的内容:
currentWindow = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, null, "Options");
Run Code Online (Sandbox Code Playgroud)
在此示例中,hwnd变量是包含按钮的应用程序主窗口的句柄。
在阅读FindWindowEx的文档时,似乎有多种使用不同参数的方法,我觉得我已经尝试了所有方法。我已经在类参数中尝试了“ NetUISimpleButton”以及“ Button”。我已经在窗口名称以及控件“ Options”的名称中尝试了null,如上所述。我在这两个字段中尝试了这些值的每种组合。我还尝试过指定child的值,使其成为主窗口的子级。我什至尝试将IntPtr.Zero用作第一个参数。
我开始认为这个hwnd:0x0是一个不祥的预兆,这意味着我根本无法选择访问此按钮。如果是这样,我还有其他选择吗?我只是想在应用程序中打开一个辅助窗口,以便可以再单击几下按钮并选择单选。
使用UI Automation非常简单。如果有人感兴趣,这是我使用的代码。
int hwnd = FindWindow("CommunicatorMainWindowClass", null);
AutomationElement lync = AutomationElement.FromHandle((IntPtr)hwnd);
AutomationElement optionsButton = lync.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Options"));
((InvokePattern)optionsButton.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
Run Code Online (Sandbox Code Playgroud)