ok, I found a lot of messages about finding a window by name, etc. What I did not find is how to find and switch the focus of the application window to the last active window. The code below will give me a list of active applications in the active task manager.
What I cannot figure out how to do is find out which application was the last active application and then switch to it. eg...
I have my own winform open application.
I press the button
My application switches to the last active window / application.
Here is the working code that I have. (this action is on the button, and it expects the application to have a text box named textbox1, you will also need to add using System.Diagnostics;
private void button1_Click(object sender, EventArgs e) { Process[] procs = Process.GetProcesses(); IntPtr hWnd; foreach (Process proc in procs) { if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero) { textBox1.Text += (proc.ProcessName.ToString()); textBox1.Text += "\t"; textBox1.Text += (hWnd.ToString()); textBox1.Text += "\r\n"; } } }
source share