Vaibhav Gaikwad
Microsoft .NET developer
Bla Code! Bla Code!

User32's SetForegroundWindow() API in C#

Thursday, November 1, 2007 3:19 AM

If there occurs a need to check if some process is already running and then bringing that process' main window in front then .NET does not helps 100%.

Using the System.Diagnositics.Process class to find out a already running process is a easy task but to bring that process' main window in front is a difficult goal.

Here's the code to get the already running process (e.g. Notepad) and then bringing it to front, using the native API from User32.dll >> SetForegroundWindow(int WindowHandle)

[System.Runtime.InteropServices.

class Program
{
static void Main(string[] args)
{
 System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
   if (p.Length > 0)
   {
     SetForegroundWindow(p[0].MainWindowHandle);
   }
 }
}

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

-Vaibhav Gaikwad




Feedback

# re: User32's SetForegroundWindow() API in C#

This does not work if you calling form has TopMost set to true.......... 1/22/2008 4:47 AM | Someone

# re: User32's SetForegroundWindow() API in C#

> This does not work if you calling form has TopMost set to true..........

Dumb comment, if TopMost no need to call SetForegroundWindow. 11/10/2008 12:23 PM | Zdis

# re: User32's SetForegroundWindow() API in C#

can you tell me how to use [DllImport("user32.dll")] though i am using the namespace using System.Runtime.InteropServices its not wokring for me..
can u please help me.

Thanks in Advance. 11/13/2008 9:46 AM | dotnetdev

# re: User32's SetForegroundWindow() API in C#

Sure I'll try my best, can you send your code snippet @ vaibhav.gaikwad[at]gmail[dot]com

Else you'll have to explain what is exactly not working for you. The System.Runtime.InteropServices is the correct namespace, but if something is not working I need to know what are you doing in the code and what kind of application is your's. 11/13/2008 11:21 AM | Vaibhav

# re: User32's SetForegroundWindow() API in C#

Thanks man .it is working fine 4/30/2009 6:08 AM | kedar

# re: User32's SetForegroundWindow() API in C#

Yes,
If you calling form has TopMost set to true,This does not work ! 7/8/2009 3:16 AM | dongyang

# re: User32's SetForegroundWindow() API in C#

That works great !!! thanks.


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

calling in my Form1 as :

SetForegroundWindow(this.Handle); 11/12/2009 4:22 AM | titwan

# re: User32's SetForegroundWindow() API in C#

Thank you! The following code may be useful to someone. It starts, then stops an app, based on the process handle, stopping based on a timer:
// member
Process activatedApp = null;

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

// in 'activate' button click handler, connected to a timer the user gives input for
activatedApp = Process.Start(AppExePath.Text.Trim());

// in the CloseApp handler (connected to another timer)
SetForegroundWindow(activatedApp.MainWindowHandle);
if (activatedApp.Responding)
{
SendKeys.SendWait("%F"); // "%" represents the Alt key; "^" Cntrl and "+" Shift
SendKeys.SendWait("x");
}
else
{
activatedApp.Kill();
}

Salut!

1/23/2011 3:20 PM | McG

# re: User32's SetForegroundWindow() API in C#

A slight mistake in my commenting above: The 'activate' button handler starts a timer based on user input and the line:
activatedApp = Process.Start(AppExePath.Text.Trim());
responds to that timer.

Cheers 1/23/2011 3:23 PM | McG

# re: User32's SetForegroundWindow() API in C#

Hello,
I have this problem.
SetForegroundWindow is still unrecognized by the VS.NET compiler of C# even if I implement user32.dll as follow
using System.Runtime.InteropServices;
and
[DllImport("user32.dll")]
SetForegroundWindow(_hWndHAP);
Error: SetForegroundWindow Not exist in the current context
Can someone suggest...pls 10/3/2011 3:13 AM | Elias

# re: User32's SetForegroundWindow() API in C#

Thank You. It worked 3/20/2012 6:58 PM | Ankur

# re: User32's SetForegroundWindow() API in C#


[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd); 4/3/2012 8:16 PM | Jamie

# re: User32's SetForegroundWindow() API in C#

I know this is an old post but the comment about TopMost and foreground window being the same is not true. It is possible to be the topmost window without being the one in focus (the input that has the input focus). 7/15/2012 12:09 AM | Craig

Post a comment