Switch to the last active application, for example, Alt-Tab

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"; } } } 
+4
source share
2 answers

Since my comments did not help you, here is a little summary (not tested):

 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")] static extern IntPtr GetLastActivePopup(IntPtr hWnd); [DllImport("user32.dll", ExactSpelling = true)] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); const uint GA_PARENT = 1; const uint GA_ROOT = 2; const uint GA_ROOTOWNER = 3; public IntPtr GetPreviousWindow() { IntPtr activeAppWindow = GetForegroundWindow(); if ( activeAppWindow == IntPtr.Zero ) return IntPtr.Zero; IntPtr prevAppWindow = GetLastActivePopup(activeAppWindow); return IsWindowVisible(prevAppWindow) ? prevAppWindow : IntPtr.Zero; } public void FocusToPreviousWindow() { IntPtr prevWindow = GetPreviousWindow(); if ( prevWindow != IntPtr.Zero ) SetForegroundWindow(prevWindow); } 
+1
source

Check out this article: http://www.whitebyte.info/programming/how-to-get-main-window-handle-of-the-last-active-window

In particular, this code:

 [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); enum GetWindow_Cmd : uint { GW_HWNDFIRST = 0, GW_HWNDLAST = 1, GW_HWNDNEXT = 2, GW_HWNDPREV = 3, GW_OWNER = 4, GW_CHILD = 5, GW_ENABLEDPOPUP = 6 } [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hWnd); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); [...] IntPtr targetHwnd = GetWindow(Process.GetCurrentProcess().MainWindowHandle, (uint)GetWindow_Cmd.GW_HWNDNEXT); while (true) { IntPtr temp = GetParent(targetHwnd); if (temp.Equals(IntPtr.Zero)) break; targetHwnd = temp; } SetForegroundWindow(targetHwnd); 
+5
source

Source: https://habr.com/ru/post/1398640/


All Articles