【问题标题】:Enumerate all windows which are "always on top" [closed]枚举所有“始终在顶部”的窗口 [关闭]
【发布时间】:2015-01-21 22:17:21
【问题描述】:

我想为我们的 Web 测试服务器编写一个实用程序,它可以杀死所有具有“始终在顶部”窗口的进程。我猜它是窗口样式WS_EX_TOPMOST,但是,我不是 100% 确定。

C#中有没有办法:

  • 枚举所有“始终位于顶部”的窗口
  • 并检索相应的进程?

【问题讨论】:

  • 所以你想检查每个进程,看看它是否有一个带有 Always on Top 标志的窗口?
  • 我更喜欢更高效、更直接的方式。如果 Windows 不提供这种方式,我想,我必须这样做......
  • pinvoke EnumWindow 并检查 WS_EX_TOPMOST

标签: c# windows window kill-process


【解决方案1】:

这是一个工作示例,它查找所有具有最顶层窗口的进程。不过要小心:Windows 资源管理器总是有一个最顶层的窗口,您可能不想想要终止该进程。

class Program
{
    const int WS_EX_TOPMOST = 0x00000008;
    const int WS_VISIBLE = 0x10000000;
    const int GWL_STYLE = -16;
    const int GWL_EXSTYLE = -20;

    static void Main(string[] args)
    {
        var topmostWindowHandles = new ArrayList();
        EnumWindows(EnumWindowsCallback, topmostWindowHandles);

        var processesToKill = new HashSet<uint>();
        foreach (IntPtr hWnd in topmostWindowHandles)
        {
            uint processId = 0;
            GetWindowThreadProcessId(hWnd, out processId);
            processesToKill.Add(processId);
        }

        foreach (uint pid in processesToKill)
        {
            Process proc = Process.GetProcessById((int)pid);
            Console.WriteLine("Killing " + proc.ProcessName);
            // kill process, except explorer.exe
        }
    }

    static bool EnumWindowsCallback(IntPtr hWnd, ArrayList lParam)
    {
        int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
        int style = GetWindowLong(hWnd, GWL_STYLE);
        if ((exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST 
            && (style & WS_VISIBLE) == WS_VISIBLE)
        {
            lParam.Add(hWnd);
        }
        return true;
    }

    public delegate bool EnumWindowsProc(IntPtr hwnd, ArrayList lParam);

    [DllImport("user32.dll")]
    [return:MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ArrayList lParam);

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
}

【讨论】:

  • 谢谢,太好了。但是,我想,我误解了WS_EX_TOPMOST。我正在寻找不会失去焦点的窗口(例如,“始终保持在顶部”的 VLC)。如果另一个窗口被聚焦,您的方法找到的大多数应用程序不会停留在顶部。您是否偶然知道如何仅确定那些“始终位于顶部”的窗口?
  • WS_EX_TOPMOST 我相信是正确的。见msdn.microsoft.com/en-us/library/windows/desktop/… 另外,我们可能需要检查窗口是否可见......我正在研究下一个......
  • @D.R.我添加了可见窗口的检查。现在再试一次。
  • 谢谢,在此期间我自己也是这样做的 :-) 我仍然找到了三个应该没有的窗口:资源管理器(好的...),AppVStreamingUX(奇怪...) ipoint(奇怪...)。两者都没有我可以看到的窗户,也许我也应该检查一下大小......
  • 使用 Spy++ 工具检查(应该与 Visual Studio 一起提供)。也许这里还有另一个线索。
【解决方案2】:

这里有一个解决方案,它可以找到具有最顶层主窗口的所有进程:

class Program
{
    static void Main(string[] args)
    {
        int WS_EX_TOPMOST = 0x00000008;
        int GWL_EXSTYLE = -20;
        foreach (Process proc in Process.GetProcesses())
        {
            IntPtr hWnd = proc.MainWindowHandle;
            int res = GetWindowLong(hWnd, GWL_EXSTYLE);
            if ((res & WS_EX_TOPMOST) == WS_EX_TOPMOST)
            {
                Console.WriteLine("Topmost window found for process " + proc.ProcessName);
            }
        }
    }

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
}

这仅检查进程的主窗口是否位于最顶层。我留下这个答案是因为它可能有助于解决这个特定问题(查找最顶层的主窗口)。

【讨论】:

  • 这假设只需要检查一个进程的主窗口。这是一个可能被证明是有效的假设,但我所看到的问题中的任何内容都不支持它。
  • @hvd 这正是我在发布的代码下方所指出的。但你是对的,这是一个可能不正确的假设。
  • 啊,我看错了你的笔记。并非所有非主窗口的窗口都是子窗口。如果要检查所有顶级窗口,则需要避免完全从进程的主窗口开始。一个进程可能有多个顶级窗口,其中没有一个是任何其他的子窗口。
  • 谢谢,我更新了我的笔记。我希望@D.R.会给我反馈。
猜你喜欢
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 2011-08-23
相关资源
最近更新 更多