在VC++中,我使用EnumWindows(...)、GetWindow(...)和GetWindowLong()来获取窗口列表,并检查该窗口是否为顶部窗口(没有其他窗口作为所有者),以及该窗口是否可见(WS_VISIBLE)。然而,虽然我的桌面只显示了5个窗口,但这个EnumWindows却给了我50个窗口,多么有趣!任何Windows的极客请帮助我澄清...
发布于 2011-09-03 18:51:19
Raymond在MSDN博客上的这篇文章中描述了在任务栏(或类似于Alt-Tab框)中列出窗口的方法:
Which windows appear in the Alt+Tab list?
这是检查窗口是否显示在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;
}在这里归功于源代码:
http://www.dfcd.net/projects/switcher/switcher.c
发布于 2021-02-21 01:26:41
@jondinham提供的answer确实非常适合我。所以我想出了我自己的解决方案。
1.我在以前的解决方案中遇到的问题
在Windows10家庭版1909上运行时,我得到了两个额外的意外Windows“计算器”和“设置”。
另外,检测不到Tencent QQ的窗口,原因如下:
// 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;但是,我认为这个bug可能是由于腾讯QQ的特殊性造成的,我甚至不能用DeferWindowPos把它的窗口放在最上面。
也许有人可以帮助我找出为什么会发生这种情况,并帮助@jondinham改进之前的解决方案。
2.我的解决方案
我试图检查窗口的图标,并过滤掉没有自己的图标或使用与系统默认图标相同的图标的窗口。我使用了来自answer和answer的代码片段,并做了一些修改。这个解决方案对我来说非常有效。
HICON get_windows_HICON_critical(HWND hwnd)
{
// Get the window icon
HICON icon = reinterpret_cast<HICON>(::SendMessageW(hwnd, WM_GETICON, ICON_SMALL, 0));
if (icon == 0) {
// Alternative method. Get from the window class
icon = reinterpret_cast<HICON>(::GetClassLongPtrW(hwnd, GCLP_HICONSM));
}
// Alternative method: get the first icon from the main module (executable image of the process)
if (icon == 0) {
icon = ::LoadIcon(GetModuleHandleW(0), MAKEINTRESOURCE(0));
}
// // Alternative method. Use OS default icon
// if (icon == 0) {
// icon = ::LoadIcon(0, IDI_APPLICATION);
// }
if(icon == ::LoadIcon(0, IDI_APPLICATION)){
// Filter out those with default icons
icon = 0;
}
return icon;
}
static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {
int length = GetWindowTextLength(hWnd);
char* buffer = new char[length + 1];
GetWindowText(hWnd, buffer, length + 1);
std::string windowTitle(buffer);
// List visible windows with a non-empty title
if (IsWindowVisible(hWnd) && length != 0) {
HICON icon = get_windows_HICON_critical(hWnd);
if(icon!=0){
std::cout << hWnd << ": " << windowTitle << std::endl;
}
}
return TRUE;
}3.我的解决方案的问题
根据this question的说法,我的解决方案无法处理Windows Store应用程序。
https://stackoverflow.com/questions/7277366
复制相似问题