Set another application focus
Hi,
I'm try to set another application, outside myapplication, as focused window.
Can someone gimmy some help? I've allready tryed something like this
System.Diagnostics.Process.GetProcessesByName("notepad");
I don't start the application in myapplication so I can't set a handle to it.
Greetz Otto
Re: Set another application focus
Get the hWnd to the app's top level window and pinvoke to SetForegroundWindow.
Re: Set another application focus
Thanks, I worked it all out.
//Import the SetForeground API to activate it
[DllImport("User32.dll")]
private static extern IntPtr SetForegroundWindow(int hWnd);
public MyProjectFunctions()
{
}
public void SetWindowFocus(string processName)
{
Process[] pname = Process.GetProcessesByName(processName);
if (pname.Length > 0)
{
foreach (Process processes in pname)
{
if (processes.ProcessName.Equals(processName))
{
SetForegroundWindow((int)processes.MainWindowHandle);
}
}
}
}
Re: Set another application focus
Just to add my 2 cents.
For some inexplicable reason I have had problems with using only SetForeGroundWindow. I also then made use of the BringWindowToTop API, which assisted in bringing the necessary window to the front. :)