User32's SetForegroundWindow() API in C#

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Thursday, November 01, 2007 3:19 AM
Print

Comments on this entry:

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

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

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

Left by Zdis at 11/10/2008 12:23 PM
Gravatar
> This does not work if you calling form has TopMost set to true..........

Dumb comment, if TopMost no need to call SetForegroundWindow.

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

Left by dotnetdev at 11/13/2008 9:46 AM
Gravatar
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.

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

Left by Vaibhav at 11/13/2008 11:21 AM
Gravatar
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.

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

Left by kedar at 4/30/2009 6:08 AM
Gravatar
Thanks man .it is working fine

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

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

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

Left by titwan at 11/12/2009 4:22 AM
Gravatar
That works great !!! thanks.


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

calling in my Form1 as :

SetForegroundWindow(this.Handle);

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

Left by McG at 1/23/2011 3:20 PM
Gravatar
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!

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

Left by McG at 1/23/2011 3:23 PM
Gravatar
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

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

Left by Elias at 10/3/2011 3:13 AM
Gravatar
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

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910