Bringing a Windows Application to the Foreground using .net

Standard

image

It’s surprising that there’s very little information available about bringing another windows application to the foreground.

The SetForegroundWindow api call can be useful in this regard, but it can’t be used unless you know the windows handle of the main window of the application you’re activating.  If the application you’re activating has many child windows, simply using the “MainWindowHandle” property on the Process object is not enough.

The code fragment and sample will show how to find a process, enumerate it’s child windows, then send these the foreground.

 

BringAppToForeground(“notepad”)

………

using System.Diagnostics; 
using System.Runtime.InteropServices;

……….

[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); 
[DllImport("user32.dll")] 

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,

string windowClass, string windowTitle);

       
private void BringAppToForeground(string appName) 
{

           // Find Parent 
           IntPtr parenthWnd = GetProcessWindowHandle(appName); 
           if (parenthWnd == IntPtr.Zero) return; 
           
           // Get list of child windows 
           List<IntPtr> loChildWindows = GetChildWindowHandles(parenthWnd);

           // Bring Windows to Front 
           BringWindowsToFront(loChildWindows.Reverse<IntPtr>());

}

private static IntPtr GetProcessWindowHandle(string processName) 
{ 
           IntPtr parenthWnd = IntPtr.Zero; 
           foreach (Process loProcess in Process.GetProcessesByName(processName)) 
           { 
               parenthWnd = loProcess.MainWindowHandle; 
               if (parenthWnd != IntPtr.Zero) break; 
           } 
           return parenthWnd; 
}

private static void BringWindowsToFront(IEnumerable<IntPtr> windows) 
{ 
           // Go through each and bring to front 
           foreach (IntPtr fronthWnd in windows) 
           { 
               SetForegroundWindow(fronthWnd); 
           } 
}

private static List<IntPtr> GetChildWindowHandles(IntPtr parenthWnd) 
{ 
           IntPtr hWnd = IntPtr.Zero; 
           List<IntPtr> loChildWindows = new List<IntPtr>(); 
           do 
           { 
               hWnd = FindWindowEx(parenthWnd, hWnd, null, null); 
               if (hWnd == IntPtr.Zero) break; 
               loChildWindows.Add(hWnd); 
           } 
           while (hWnd != IntPtr.Zero); 
           return loChildWindows; 
} 


To understand how to use this in a winforms project, take a look at the sample project.

Download Sample Project

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s