If you want to reliable bring the main window of an external process to the foreground in C#, use the “simulate alt key” technique found at: http://stackoverflow.com/questions/10740346/setforegroundwindow-only-working-while-visual-studio-is-open

Just start Notepad, then run the following test in Visual Studio.

It should bring Notepad as a maximized window in the foreground.

namespace Research.EndToEndTests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    [TestClass]
    public class ResearchTester
    {
        
        [TestMethod]
        public void Test()
        {
            Reliable_set_window_to_forground();
            Assert.IsTrue(true);
        }

        public void Reliable_set_window_to_forground()
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process proc in processes)
            {
                if (ProcessIsNotepad(proc))
                {
                    ActivateWindow(proc.MainWindowHandle);
                }
            }
        }

        public bool ProcessIsNotepad(Process proc) 
        {
            return proc.MainWindowTitle.EndsWith("Notepad", StringComparison.InvariantCultureIgnoreCase);
        }
        
        private const int ALT = 0xA4;
        private const int EXTENDEDKEY = 0x1;
        private const int KEYUP = 0x2;
        private const int SHOW_MAXIMIZED = 3;

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        
        public static void ActivateWindow(IntPtr mainWindowHandle)
        {
            // Guard: check if window already has focus.
            if (mainWindowHandle == GetForegroundWindow()) return;

            // Show window maximized.
            ShowWindow(mainWindowHandle, SHOW_MAXIMIZED);
            
            // Simulate an "ALT" key press.
            keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0);
                        
            // Simulate an "ALT" key release.
            keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0);

            // Show window in forground.
            SetForegroundWindow(mainWindowHandle);
        }
    }
}

3 Comments

  1. It is working only notepad . when Main window handle is greater than Max set point. this not working . showwindow method not working . any solution .

    Piyush Ratan Sharma
  2. is there a way to get this to work with windows 10 marketplace apps (or the new universal apps?)

    the switch to window works fine with classic windows desktop apps but i cant seem to bring windows 10/marketplace apps to the foreground.

    VK
  3. Thanks this works great for me.
    After trying a few things that did not work I tried this.
    I wanted to popup a winforms over chrome browser that was in kiosk mode and it worked.
    Thanks for sharing.

    Stephan

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.