windows 为什么 EnumWindows 返回的窗口比我预期的多?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7277366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:59:02  来源:igfitidea点击:

Why does EnumWindows return more windows than I expected?

c++windowsvisual-c++styles

提问by jondinham

In VC++, I use EnumWindows(...), GetWindow(...), and GetWindowLong(), to get the list of windows and check whether the window is top window (no other window as owner), and whether the window is visible (WS_VISIBLE). However, although my desktop is showing only 5 windows, this EnumWindows is giving me 50 windows, how funny! Any Windows geek here please help me clarify...

在 VC++ 中,我使用 EnumWindows(...)、GetWindow(...) 和 GetWindowLong() 来获取窗口列表并检查窗口是否是顶部窗口(没有其他窗口作为所有者),以及窗口是否可见 (WS_VISIBLE)。然而,虽然我的桌面只显示了 5 个窗口,但这个 EnumWindows 给了我 50 个窗口,多有趣!任何 Windows 极客请帮我澄清...

回答by jondinham

The way to list out only windows in taskbar (or similarly in Alt-Tab box) is described by Raymond in this article on MSDN blog:

Raymond 在 MSDN 博客上的这篇文章中描述了在任务栏中(或类似地在 Alt-Tab 框中)仅列出窗口的方法:

Which windows appear in the Alt+Tab list?

哪些窗口出现在 Alt+Tab 列表中?

And this is the super function to check whether a window is shown in alt-tab:

这是检查窗口是否显示在 alt-tab 中的超级函数:

BOOL IsAltTabWindow(HWND hwnd)
{
    TITLEBARINFO ti;
    HWND hwndTry, hwndWalk = NULL;

    if(!IsWindowVisible(hwnd))
        return FALSE;

    hwndTry = GetAncestor(hwnd, GA_ROOTOWNER);
    while(hwndTry != hwndWalk) 
    {
        hwndWalk = hwndTry;
        hwndTry = GetLastActivePopup(hwndWalk);
        if(IsWindowVisible(hwndTry)) 
            break;
    }
    if(hwndWalk != hwnd)
        return FALSE;

    // the following removes some task tray programs and "Program Manager"
    ti.cbSize = sizeof(ti);
    GetTitleBarInfo(hwnd, &ti);
    if(ti.rgstate[0] & STATE_SYSTEM_INVISIBLE)
        return FALSE;

    // Tool windows should not be displayed either, these do not appear in the
    // task bar.
    if(GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW)
        return FALSE;

    return TRUE;
}

Credited to the source code here:
http://www.dfcd.net/projects/switcher/switcher.c

归功于此处的源代码:http:
//www.dfcd.net/projects/switcher/switcher.c

回答by Seth Carnegie

The windows that you are talking about, with an X button and a title bar, etc. are not the only kind of windows. Buttons, dropdown menus, labels, icons, text boxes, the task bar, and just about everything else is a window too1. So EnumWindowsis doing exactly what it's supposed to do: enumerate all the top level windows.

您所说的带有 X 按钮和标题栏等的窗口并不是唯一的窗口类型。按钮、下拉菜单、标签、图标、文本框、任务栏以及几乎所有其他内容也是一个窗口1。所以EnumWindows正是做它应该做的:枚举所有顶级窗口。

1Even though this is true, EnumWindowsonly enumerates the top level windows. That means it won't enumerate any child windows:

1即使这是真的,也EnumWindows只枚举顶级窗口。这意味着它不会枚举任何子窗口

The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style.

EnumWindows 函数不枚举子窗口,但系统拥有的少数具有 WS_CHILD 样式的顶级窗口除外。

However, many things on your desktop are windows as well, not just the "windows" you're thinking about.

但是,桌面上的许多东西也是窗口,而不仅仅是您正在考虑的“窗口”。