获取另一个窗口中的按钮的句柄

时间:2014-06-13 16:15:03

标签: c# winapi

使用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 ]

关于这个按钮的特殊之处在于它没有hwnd值。 (HWND:为0x0)。这是我试图获得对按钮的引用:

currentWindow = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, null, "Options");

在此示例中,hwnd变量是包含按钮的应用程序主窗口的句柄。

在阅读FindWindowEx的文档时,看起来好像有多种方法可以使用不同的参数,我觉得我已经尝试了所有这些。我在类参数中尝试了“NetUISimpleButton”,以及“Button”。我已经在窗口名称中尝试了null,以及控件“选项”的名称,如上所示。我已经尝试过这两个字段中每个值的组合。我还尝试为子窗口的子窗口后指定一个值。我甚至尝试将IntPtr.Zero用于两个第一个参数。

我开始认为这个hwnd:0x0是一个不祥之兆,这意味着我根本无法获得访问此按钮的选项。如果是这种情况,我还有其他选择吗?我只是想在应用程序中打开一个辅助窗口,以便我可以再按几次按钮和选择无线电。

1 个答案:

答案 0 :(得分:4)

UI自动化非常简单。这是我用过的代码,如果有人感兴趣的话。

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();