Bring the window to Front and set It Active window using c#

using this Fucntion we can Bring the window to Front and set It Active window. i'm using win API .


[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);

private void BringToFront(string className,string CaptionName)
{
SetForegroundWindow(FindWindow(className,CaptionName));
}



private void Form1_Load(object sender, EventArgs e)
{

BringToFront("Notepad", "Untitled - Notepad");

}


Comments

Author: Victor Birin24 Feb 2008 Member Level: Silver   Points : 0

You don't need to use FindWindow call. Just replace in with Handle.ToInt32();

result

SetForegroundWindow(Handle.ToInt32());

Author: Bernd Wechner17 Aug 2010 Member Level: Bronze   Points : 1

I find that this works more simply:

TopMost = true;
TopMost = false;

I don't want the form to stay topmost so I turn it off again imemdiately, but I find that setting TopMost actually displays it and works around the annoying habit Windos has of flashing the taskbar button instead.