Tab最后一个活动的应用程序

Tab最后一个活动的应用程序

本文介绍了切换到像使用Alt-Tab最后一个活动的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我发现有很多帖子上的名字找到一个窗口,等我还没有发现是如何找到和切换窗口的应用程序集中到最后一个活动窗口。我下面展示的代码都会给我的任务管理器是活动的活跃应用程序列表。

ok, I have found many posts on finding a window by name, etc. What I have not found is how to find and switch the window application focus to last active window. The code I am showing below will give me the list of active applications in the task manager that are active.

我想不出什么如何做的是弄清楚什么应用程序是最后一个活动的应用程序,然后切换到它。例如...

What I can not figure out how to do is figure out what application was the last active application, and then switch to it. for example...

我有我的自定义WinForm应用程序打开。

I have my custom winform application open.

我点击一个按钮

我的应用程序切换到最后一个活动窗口/应用程序。

My application switches to the last active window / application.

下面是工作代码,我到目前为止。 (这是一个按钮的动作,并预计该应用程序有一个名为TextBox1的文本框,你还需要使用System.Diagnostics程序添加;

Here is the working code I have so far. (this is the action on a button, and it expects that the application has a textbox 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";
            }
        }

    }


推荐答案

检查这篇文章出来:的

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

具体而言,这段代码:

[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);

这篇关于切换到像使用Alt-Tab最后一个活动的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:43