首页 > 解决方案 > 如何从子窗口句柄获取主窗口句柄

问题描述

我想从前景窗口处理主窗口。

hWnd = GetForegroundWindow();

hwndOwner = GetWindow(hWnd, GW_OWNER);
hwndParent = GetParent(hWnd);

hwndOwner并且hwndParent只是前景窗口上方的一层。如何在不递归调用上述函数的情况下获得主窗口句柄?

标签: c++winapi

解决方案


使用带有 GA_ROOT 标志的 GetAncestor:GetAncestor(hwndChild, GA_ROOT);

请注意,我不确定 GA_ROOT 和 GA_ROOTOWNER 之间的区别。


推荐阅读