【问题标题】:Getting a Handle to a button in another window获取另一个窗口中按钮的句柄
【发布时间】:2014-06-14 01:27:50
【问题描述】:

使用 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 是一个不祥之兆,这意味着我根本无法访问这个按钮。如果是这种情况,我还有其他选择吗?我只是想在应用程序中打开一个辅助窗口,以便我可以进行更多的按钮点击和单选。

【问题讨论】:

  • 有些按钮不是窗口,所以它们没有句柄。您需要使用UI Automation
  • 太棒了。效果很好!我将在下面的答案中发布我的代码。

标签: c# winapi


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    相关资源
    最近更新 更多