【问题标题】:Retrieve order of open windows as ordered in the Alt-Tab list?按 Alt-Tab 列表中的顺序检索打开窗口的顺序?
【发布时间】:2017-04-07 13:27:08
【问题描述】:

这是我的 C# 应用程序的完整代码,目标很简单。我想检索系统上打开的窗口,按照它们最近打开的方式排序,就像在 Alt-Tab 列表中一样。 Alt-Tab 列表列出了上次打开的程序,因此按 Alt-Tab 并仅释放一次将带您回到您打开的最后一个窗口。此代码适用于 Windows 10。下面的代码确实获得了我需要的信息,只是顺序不正确。我应该在哪里寻找我需要的信息?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace GetOpenWindowName
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] processlist = Process.GetProcesses();

            foreach (Process process in processlist)
            {
                if (!String.IsNullOrEmpty(process.MainWindowTitle))
                {
                    Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
                }
            }

            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 此信息不可用。您必须自己监控窗口激活。
  • @PaulF 这可能会很有帮助,谢谢。
  • 相关(从 2003 年开始)-What is the Alt+Tab order?
  • Z-order 不一定反映激活顺序。具有WS_EX_TOPMOST 扩展窗口样式的窗口将破坏 Z 顺序和激活历史记录之间的关系。

标签: c# windows windows-10 system.diagnostics


【解决方案1】:

所以,在 @PaulF、@stuartd、 和 @IInspectible 的帮助下,这是我能做到的最好的事情了。

Alt-Tab 列表中窗口的顺序大致是窗口的 z 顺序。 @IInspectible 告诉我们,设置为最顶层的窗口会破坏这一点,但在大多数情况下,z 顺序可以得到遵守。所以,我们需要得到打开窗口的z-order。

首先,我们需要引入外部函数GetWindow,使用这两行代码:

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetWindow(IntPtr hWnd, int nIndex);

一旦该函数存在,我们就可以创建该函数来获取 z 顺序:

public static int GetZOrder(Process p)
{
    IntPtr hWnd = p.MainWindowHandle;
    var z = 0;
    // 3 is GetWindowType.GW_HWNDPREV
    for (var h = hWnd; h != IntPtr.Zero; h = GetWindow(h, 3)) z++;
    return z;
}

重点:调用GetWindow函数中的三个是一个标志:

/// <summary>
/// The retrieved handle identifies the window above the specified window in the Z order.
/// <para />
/// If the specified window is a topmost window, the handle identifies a topmost window.
/// If the specified window is a top-level window, the handle identifies a top-level window.
/// If the specified window is a child window, the handle identifies a sibling window.
/// </summary>
GW_HWNDPREV = 3,

这些是从进程列表中查找窗口 z 顺序的构建块,这(在大多数情况下)是 Alt-Tab 顺序。

【讨论】:

    【解决方案2】:

    实现 EnumWindows 似乎按 Tab 键顺序返回窗口

    [DllImport("user32.dll")]
    private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
    

    这里有一个很好的解释如何使用 How can I use EnumWindows to find windows with a specific caption/title?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      相关资源
      最近更新 更多