从两个窗口返回前景窗口
Return the foreground window from two windows
在win32
编程中,给定两个重叠的窗口,w1
和w2
,如何获取前景窗口?
GetForegroundWindow()
为您提供实际的前景窗口(具有当前焦点的窗口(。前景中一次只能有 1 个窗口。
如果 2 个窗口都不是前景窗口,则没有 API 可用于直接确定哪个窗口在 z 顺序上高于另一个窗口。您必须通过使用EnumWindows()
和EnumChildWindows()
枚举窗口来手动确定这一点。 窗口根据其 z 顺序进行枚举。
例如:
struct myEnumInfo
{
HWND hwnd1;
HWND hwnd2;
HWND hwndOnTop;
};
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
myEnumInfo *info = (myEnumInfo*) lParam;
// is one of the HWNDs found? If so, return it...
if ((hwnd == info->hwnd1) || (hwnd == info->hwnd2))
{
info->hwndOnTop = hwnd;
return FALSE; // stop enumerating
}
return TRUE; // continue enumerating
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
myEnumInfo *info = (myEnumInfo*) lParam;
// is one of the HWNDs found? If so, return it...
if ((hwnd == info->hwnd1) || (hwnd == info->hwnd2))
{
info->hwndOnTop = hwnd;
return FALSE;
}
// enumerate this window's children...
EnumChildWindows(hwnd, &EnumChildProc, lParam);
// is one of the HWNDs found? If so, return it...
if (info->hwndOnTop)
return FALSE; // stop enumerating
return TRUE; // continue enumerating
}
HWND WhichOneIsOnTop(HWND hwnd1, HWND hwnd2)
{
// is one of the HWNDs null? If so, return the other HWND...
if (!hwnd1) return hwnd2;
if (!hwnd2) return hwnd1;
// is one of the HWNDs in the actual foreground? If so, return it...
HWND fgWnd = GetForegroundWindow();
if ((fgWnd) && ((fgWnd == hwnd1) || (fgWnd == hwnd2)))
return fgWnd;
myEnumInfo info;
info.hwnd1 = hwnd1;
info.hwnd1 = hwnd2;
info.hwndOnTop = NULL;
// are the HWNDs both children of the same parent?
// If so, enumerate just that parent...
HWND parent = GetAncestor(hwnd1, GA_PARENT);
if ((parent) && (GetAncestor(hwnd2, GA_PARENT) == parent))
{
EnumChildWindows(parent, &EnumChildProc, (LPARAM)&info);
}
else
{
// last resort!! Enumerate all top-level windows and their children,
// looking for the HWNDs wherever they are...
EnumWindows(&EnumWindowsProc, (LPARAM)&info);
}
return info.hwndOnTop;
}
相关文章:
- 从两个窗口返回前景窗口
- 返回主窗口/ C ++后,应用程序正在关闭
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- QT-如何通过按下按钮从子窗口小部件返回到父窗口小部件
- GetWindowRect()返回的大小小于游戏的实际可见窗口的可能原因是什么
- 将窗口的样式从标准更改为没有标题栏和返回
- 来自程序子窗口的位图返回为黑色
- SDL窗口在启动时关闭并返回0
- 标准容器/库,用于存储数字窗口并返回唯一数字的数量
- 当在拖动/调整窗口大小的同时对窗口调用isActive()时,返回false.(X11,问题5.4.0)
- NTP服务器总是返回相同的错误日期(窗口)
- 使用 BitBlt 捕获程序窗口始终返回相同的图像
- IsWindowVisible() 是否可以在 Excel 窗口打开时返回 false
- 防止控制台窗口在返回值后自行关闭
- IsIconic()总是返回false,而OpenIcon()从不打开窗口
- CreateWindowEx返回null:无法创建窗口
- 返回"file already exists"错误的窗口类的注册类
- 将焦点设置到父窗口,然后返回到子窗口
- 我怎么能返回信息后,我的主窗口在Qt关闭
- 创建一个向主窗体返回值的模态窗口