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
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();
}
}