16

I am assuming I need to use pinvoke but I am not sure which function calls are needed.

Scenario: a legacy application will be running, I will have Handle for that application.

I need to:

  1. bring that application to the top (in front of all other windows)
  2. make it the active window

Which Windows function calls are needed?

bluish
  • 26,356
  • 27
  • 122
  • 180
Gerald Davis
  • 4,541
  • 2
  • 31
  • 47

3 Answers3

16

If you don't have a handle to the window, use this before :

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Now assuming you have a handle to the application window :

[DllImport("user32.dll", SetLastError = true)]
static extern bool SetForegroundWindow(IntPtr hWnd);

This will make the taskbar flash if another window has keyboard focus.

If you want to force the window to come to the front, use ForceForegroundWindow (sample implementation).

user703016
  • 37,307
  • 8
  • 87
  • 112
  • SetForegroundWindow seems to do what I need so far however I get error when trying to call ForceForegroundWindow ("Unable to find an entry point named 'ForceForegroundWindow' in DLL 'user32.dll'.") – Gerald Davis Jun 03 '11 at 15:07
  • In my case, I had to use 'BringWindowToTop' in addition to this. ForceForegroundWindow solved my problem beautifully. Thanks! – Robert Jeppesen Jun 05 '12 at 20:10
  • Is this the ForceForegroundWindow code you mentioned: http://pastebin.com/zkdZvKuG ? – DDA Mar 07 '14 at 18:16
  • @user1681490 Sadly I don't remember what the original code was, but it's possible, yes. – user703016 Mar 07 '14 at 19:28
  • @ParkYoung-Bae how would I include/use the WinAPI class in C#? – andreas Jan 04 '15 at 00:52
  • For what it's worth, I found the ForceForegroundWindow full example here: https://shlomio.wordpress.com/2012/09/04/solved-setforegroundwindow-win32-api-not-always-works/ via http://pinvoke.net/default.aspx/user32/SetForegroundWindow.html I can't seem to get it to work in Win 10 v1903, but it could just be the application I'm working to bring forward. – cvocvo Nov 20 '19 at 18:44
15

This has proved to be extremely reliable. The ShowWindowAsync function is specifically designed for windows created by a different thread. The SW_SHOWDEFAULT makes sure the window is restored prior to showing, then activating.

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool ShowWindowAsync(IntPtr windowHandle, int nCmdShow);

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool SetForegroundWindow(IntPtr windowHandle);

Then making the calls:

ShowWindowAsync(windowHandle, SW_SHOWDEFAULT);
ShowWindowAsync(windowHandle, SW_SHOW);
SetForegroundWindow(windowHandle);
rigamonk
  • 1,179
  • 2
  • 17
  • 45
12
    [DllImport("user32.dll")]
    public static extern bool ShowWindowAsync(HandleRef hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr WindowHandle);
    public const int SW_RESTORE = 9;

ShowWindowAsync method is used to show the minimized application and SetForegroundWindow method is used to bring on front the back application.

you can use these methods as i used in my application to bring the skype infront of my application. on button click

private void FocusSkype()
    {
        Process[] objProcesses = System.Diagnostics.Process.GetProcessesByName("skype");
        if (objProcesses.Length > 0)
        {
            IntPtr hWnd = IntPtr.Zero;
            hWnd = objProcesses[0].MainWindowHandle;
            ShowWindowAsync(new HandleRef(null,hWnd), SW_RESTORE);
             SetForegroundWindow(objProcesses[0].MainWindowHandle);
        }
    }
Talha
  • 18,898
  • 8
  • 49
  • 66