【问题标题】:Capture the handle of Dialog Button click in WPF from another application从另一个应用程序捕获 WPF 中单击对话框按钮的句柄
【发布时间】:2019-12-03 12:19:38
【问题描述】:

我正在尝试在其他 Winform 应用程序中捕获 WPF 应用程序的对话框按钮单击句柄。

我得到了Capture button click inside a messagebox in another application的帮助。

如果它是 Windows 窗体应用程序,我将成功捕获对话框单击的句柄。但我的主要目标是从我无法捕获的 WPF 应用程序中捕获它。

我的代码如下,

private void StartAppWatcher(string elementName, FindWindowMethod method)
    {
        windowElement = GetAppElement(elementName, method);
        // (...)
        // You may want to to something if the watched application is already running when you start your app

        Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement,
            TreeScope.Subtree, (elm, e) => {
                AutomationElement element = elm as AutomationElement;

                try
                {
                    if (element == null || element.Current.ProcessId == currentProcessId) return;
                    if (windowElement == null) windowElement = GetAppElement(elementName, method);
                    //if (windowElement == null || windowElement.ProcessId != element.Current.ProcessId) return;

                    // If the Window is a MessageBox generated by the watched app, attach the handler
                    if (element.Current.Name == "Dialog")
                    {
                        buttonElement = element;
                        var msgBoxButton = element.FindFirst(TreeScope.Descendants,
                            new PropertyCondition(AutomationElement.NameProperty, "OK"));
                        if (msgBoxButton != null && msgBoxButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
                        {
                            Automation.AddAutomationEventHandler(
                                InvokePattern.InvokedEvent, msgBoxButton, TreeScope.Element,
                                    DialogButtonHandler = new AutomationEventHandler(MessageBoxButtonHandler));
                        }
                    }
                }
                catch (ElementNotAvailableException)
                {
                    // Ignore: this exception may be raised if you show a modal dialog, 
                    // in your own app, that blocks the execution. When the dialog is closed, 
                    // AutomationElement element is no longer available
                }
            });

        Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, AutomationElement.RootElement,
            TreeScope.Subtree, (elm, e) => {
                AutomationElement element = elm as AutomationElement;

                if (element == null || element.Current.ProcessId == currentProcessId || windowElement == null) return;
                if (windowElement.ProcessId == element.Current.ProcessId)
                {
                    if (windowElement.MainWindowTitle == element.Current.Name)
                    {
                        windowElement = null;
                    }
                }
            });
    }

private void MessageBoxButtonHandler(object sender, AutomationEventArgs e)
    {

        MessageBox.Show("Dialog Box clicket at : " + DateTime.Now);
        Automation.RemoveAutomationEventHandler(e.EventId, buttonElement, DialogButtonHandler);
    }

【问题讨论】:

    标签: c# wpf event-handling capture handle


    【解决方案1】:

    尝试在子元素中搜索,元素类型为 Button 并忽略名称中的大小写。 此代码适用于我们:

    private const string BTN_CANCEL_NAME = "Cancel";
    private const string BTN_OK_NAME = "Ok";
    
    private static AutomationElement FindButton(AutomationElement window, string btnName)
    {
        var btnCondition = new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
                                        new PropertyCondition(AutomationElement.NameProperty, btnName, PropertyConditionFlags.IgnoreCase));
    
        return window.FindFirst(TreeScope.Children, btnCondition);
    }
    

    【讨论】:

    • 搜索不是问题。我没有得到对话框点击 wpf 应用程序的句柄,但在 windows 窗体应用程序上得到了很好的处理
    猜你喜欢
    • 2023-03-19
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2014-02-10
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多