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

C# 获取另一个窗口中按钮的句柄,c#,winapi,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

使用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”。我在窗口名和控件“Options”的名称中尝试了null,如上所示。我在这两个字段中尝试了所有这些值的组合。我还尝试为child after指定一个值,使其成为主窗口的子窗口。我甚至试着把IntPtr.0作为第一个参数


我开始认为这个hwnd:0x0是一个不祥的预兆,这意味着我根本没有访问这个按钮的选项。如果是这样,我还有其他选择吗?我只是想在应用程序中打开一个辅助窗口,这样我就可以再点击几个按钮和单选按钮。

用户界面自动化非常简单。这是我使用的代码,以防有人感兴趣

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

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

有些按钮不是窗口,因此没有手柄。你需要使用。很棒的arx。真是太棒了!我将在下面的答案中发布我的代码。有些按钮不是窗口,所以它们没有句柄。你需要使用。很棒的arx。真是太棒了!我将在下面的答案中发布我的代码。