Click to See Complete Forum and Search --> : Set another application focus


Phoney
October 29th, 2009, 08:52 AM
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

Arjay
October 29th, 2009, 01:22 PM
Get the hWnd to the app's top level window and pinvoke to SetForegroundWindow.

Phoney
October 30th, 2009, 03:17 AM
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);
}
}
}

}

HanneSThEGreaT
October 30th, 2009, 07:49 AM
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. :)