Most Efficient Way for getting notified on window open

前端 未结 3 1587
孤独总比滥情好
孤独总比滥情好 2020-12-08 23:25

I am writing an app (C# and WPF in .NET 4.0) that needs to get open windows and close them if they are not in it\'s white-list.

So far, using EnumDesktopWindo

3条回答
  •  粉色の甜心
    2020-12-09 00:22

    Use the System.Windows.Automation namespace.

    Example (taken from The Old New Thing) which waits for a specific process to open a dialog box, then dismisses it:

    using System;
    using System.Windows.Automation;
    using System.Diagnostics;
    using System.Threading;
    
    class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {     
            Automation.AddAutomationEventHandler(
                WindowPattern.WindowOpenedEvent,
                AutomationElement.RootElement,
                TreeScope.Children,
                (sender, e) =>
                {
                    var element = sender as AutomationElement;
    
                    Console.WriteLine("new window opened");
                });
    
            Console.ReadLine();
    
            Automation.RemoveAllEventHandlers();
        }
    }
    

提交回复
热议问题