EnumWindows 返回关闭的 Windows 应用商店应用程序 [英] EnumWindows returns closed Windows Store applications

查看:30
本文介绍了EnumWindows 返回关闭的 Windows 应用商店应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码:

internal static List<DetectedWindow> EnumerateWindows()
{
    var shellWindow = GetShellWindow();

    var windows = new List<DetectedWindow>();

    EnumWindows(delegate (IntPtr handle, int lParam)
    {
        if (handle == shellWindow)
            return true;

        if (!IsWindowVisible(handle))
            return true;

        if (IsIconic(handle))
            return true;

        var length = GetWindowTextLength(handle);

        if (length == 0)
            return true;

        var builder = new StringBuilder(length);

        GetWindowText(handle, builder, length + 1);
        GetWindowRect(handle, out Rect rect);

        windows.Add(new DetectedWindow(handle, rect.ToRect(), builder.ToString()));

        return true;
    }, IntPtr.Zero);

    return windows;
}

辅助类:

public class DetectedWindow
{
    public IntPtr Handle { get; private set; }

    public Rect Bounds { get; private set; }

    public string Name { get; private set; }

    public DetectedWindow(IntPtr handle, Rect bounds, string name)
    {
        Handle = handle;
        Bounds = bounds;
        Name = name;
    }
}

我得到了这个应用程序列表(窗口文本 - Rect 边界):

I'm getting this list of applications (Window text - Rect bounds):

Microsoft Visual Studio  - -8;-8;1936;1056
Microsoft Edge - 0;77;1920;963
EnumWindows - Stack Overflow and 7 more pages ‎- Microsoft Edge - -8;-8;1936;1056
Microsoft Edge - 0;77;1920;963
Microsoft Edge - 0;77;1920;963
Microsoft Edge - 0;0;1920;1080
Microsoft Edge - 0;0;1920;1080
Microsoft Edge - 0;8;1920;1040
Microsoft Edge - 0;85;1920;963
Microsoft Edge - 150;79;1532;42
Microsoft Edge - 0;85;1920;963
Microsoft Edge - 0;77;1920;963
Microsoft Edge - 0;85;1920;963
Microsoft Edge - 0;213;1920;964
Microsoft Edge - 0;0;1920;1080
Microsoft Edge - 484;208;952;174
Microsoft Edge - 0;84;1920;964
Microsoft Edge - 0;84;1920;964
Microsoft Edge - 0;84;1920;964 
Microsoft Edge - 0;0;1920;1080
Mail - 0;32;1356;693
Mail - 278;252;1372;733
OneNote - 0;8;1920;1040
My notes - OneNote - -8;-8;1936;1056
Photos - 0;32;1920;1008
Photos - -8;-8;1936;1056
Skype - 0;40;1920;1008
Skype - -8;-8;1936;1056
Store - 0;40;1920;1008
Store - -8;-8;1936;1056
Movies & TV - 0;0;1920;1080
Movies & TV - -8;-8;1936;1056
Groove Music - 0;32;1466;712
Groove Music - -7;3;1372;733
Settings - 0;40;1920;1008
Settings - -8;-8;1936;1056
Windows Shell Experience Host - 0;0;1920;1080

我当前最小化的窗口是Visual Studio 和两个 Edge 窗口(每个窗口有几个选项卡).我可以理解只有一个 Edge 项目列出了当前页面的标题这一事实.因为我最近从崩溃中恢复过来,并且只加载了那个页面.

My current not minimized windows are Visual Studio and two Edge windows (with several tabs each). I can understand the fact that only one Edge item was listing the title of the current page. Because I recently recovered from a crash and only that page was loaded.

我的问题是:

  1. 为什么我的关闭 Windows 应用商店应用会被列出?(甚至两次)
  2. 为什么会列出我的 Edge 选项卡?
  3. 如何过滤 Edge 选项卡和 关闭 Windows 应用商店应用?
  1. Why are my closed Windows Store apps being listed? (and even twice)
  2. Why are my Edge tabs being listed?
  3. How can I filter the Edge tabs and the closed Windows Store apps?

  1. 通过过滤器":仅检索具有可见窗口的应用程序.在我的用例中,只有 3 个窗口可见.

我试图让每个窗口的 WsStyle 和 WsEXStyle 进行比较,但我找不到任何区别.

I tried to get the WsStyle and WsEXStyle of each window to compare, but I couldn't find any difference.

方法 IsWindowVisible() 未能过滤掉不可见的 Windows 应用商店应用.

The method IsWindowVisible() fails filter out the Windows Store apps that are not visible.

推荐答案

为什么会列出我关闭的 Windows 应用商店应用?

Why are my closed Windows Store apps being listed?

因为它们实际上并未关闭.使用任务管理器、进程选项卡轻松查看.您将看到拥有这些窗口的进程暂停.作为 WinRT(又名 UWP、又名商店、又名现代 UI、又名 Metro)编程框架方法的一部分,现代机器拥有足够的 RAM,即使用户不与它们交互,也可以保持进程运行.使它们再次快速恢复并节省电池寿命.如果其他地方需要 RAM,那么操作系统将通过终止此类进程来挽救它.

Because they are not actually closed. Easy to see with Task Manager, Processes tab. You'll see that the process that owns these windows is suspended. Part of the WinRT (aka UWP, aka Store, aka Modern UI, aka Metro) programming framework approach, modern machines have enough RAM to make it feasible to keep processes running even if the user doesn't interact with them. Brings them back quickly again and saves battery life. If RAM is needed elsewhere then the OS will salvage it by killing such a process.

为什么会列出我的 Edge 选项卡?

Why are my Edge tabs being listed?

因为 Edge 也是一个 WinRT 应用程序.

Because Edge is a WinRT app as well.

如何过滤 Edge 选项卡和关闭的 Windows 应用商店应用?

How can I filter the Edge tabs and the closed Windows Store apps?

鉴于窗口实际上并未关闭,您要过滤的属性并不完全清楚.GetWindowThreadProcessId() 和 IsImmersiveProcess() 可以告诉您您正在处理这样一个进程.考虑 IsWindowVisible().或许这篇博文可以帮到你,也可以告诉你为什么会看到多个窗口.

It is not entirely clear by which property you want to filter, given that the window is in fact not closed. GetWindowThreadProcessId() and IsImmersiveProcess() can tell you that you are dealing with such a process. Consider IsWindowVisible(). Maybe this post can help, also tells you why you see multiple windows.

编辑(尼克·马纳林):

Edit (Nicke Manarin):

通过检查 Cloacked 属性,可以忽略隐藏/后台应用程序:

By checking the Cloacked attribute, it is possible to ignore the hidden/background Store apps:

DwmGetWindowAttribute(handle, (int)DwmWindowAttribute.Cloaked, out bool isCloacked, Marshal.SizeOf(typeof(bool)));

if (isCloacked)
    return true;

编辑 2 (Nicke Manarin):

Edit 2 (Nicke Manarin):

每个 Edge 选项卡的行为就像一个窗口(我相信这与您可以拖动选项卡以创建新窗口的事实有关).

Each Edge tab behaves like a window (I believe it has something to do with the fact that you can drag the tab to create a new window).

这篇关于EnumWindows 返回关闭的 Windows 应用商店应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆