0

I have successfully used the solution for detecting opening windows described here: UIAutomation FlaUI - Detect opening windows on application level.

However, I would also like to also detect closing window events in addition to opening window events.
I duplicated the code in order to detect the second event:

        AutomationElement root = automation.GetDesktop();

        UIA3AutomationEventHandler automationOpenEventHandler = new UIA3AutomationEventHandler(
            root.FrameworkAutomationElement,
            automation.EventLibrary.Window.WindowOpenedEvent,
            HandleOpenEvents);

        automation.NativeAutomation.AddAutomationEventHandler(
            automation.EventLibrary.Window.WindowOpenedEvent.Id,
            root.ToNative(),
            (Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
            null,
            automationOpenEventHandler);

        UIA3AutomationEventHandler automationCloseEventHandler = new UIA3AutomationEventHandler(
            root.FrameworkAutomationElement,
            automation.EventLibrary.Window.WindowClosedEvent,
            HandleCloseEvents);

        automation.NativeAutomation.AddAutomationEventHandler(
            automation.EventLibrary.Window.WindowClosedEvent.Id,
            root.ToNative(),
            (Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
            null,
            automationCloseEventHandler);

However, only one of the two (I believe the second one declared above) actually works. Note that if I declare just one of the handlers above, either the open or close, it works fine. I just don't know how to get both going.

Any help would be appreciated!

dcr
  • 1
  • The code here works fine: https://stackoverflow.com/a/63292428/403671 – Simon Mourier Jul 15 '22 at 06:04
  • Thanks, with a little modification that code worked for me. However, I would like to know how the same can be accomplished using FlaUI. – dcr Jul 15 '22 at 17:28
  • The difference is my code adds an event to the window, not the whole tree. This FlaUI thing is just a wrapper over UI automation. – Simon Mourier Jul 16 '22 at 05:17

0 Answers0