0

I am trying to capture the handle of dialog button click of a WPF application inside other Winform Application.

I have taken help from Capture button click inside a messagebox in another application.

I am successfully capturing the handle of a dialog click if it is a Windows Forms application. But my primiary objective is to capture it from WPF application for which I am unable to capture it.

My Code is as below,

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);
    }
Faran Saleem
  • 404
  • 1
  • 7
  • 31

1 Answers1

-1

Try searching in childs, element type Button and ignore case in name. This code works for us:

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);
}
Artem
  • 9
  • 3
  • Searching is not an issue.I am not getting the handle of dialog click on wpf apps, but getting them fine on windows form applicaitons – Faran Saleem Dec 03 '19 at 12:49