这是一个可以与 EnumWindows() 一起使用的回调:
BOOL CALLBACK FindTopmostWnd(HWND hwnd, LPARAM lParam)
{
HWND* pHwnd = (HWND*)lParam;
HWND myParent = hwnd;
do
{
myParent = GetParent(myParent);
}
while (myParent && (myParent != *pHwnd));
if (myParent != 0)
{
// If the window is a menu_worker window then use it's parent
TCHAR szClassName[7];
while (0 != GetClassName(hwnd, szClassName, 7)
&& 0 != _tcsncmp(szClassName, TEXT("Dialog"), 6)
&& 0 != _tcsncmp(szClassName, TEXT("Afx"), 3)
)
{
// find the worker's parent
hwnd = GetParent(hwnd);
}
*pHwnd = hwnd;
return FALSE;
}
return TRUE;
}
正如亚当指出的那样,传递给 EnumWindows() 的 LPARAM 应该是指向 HWND 的指针。所以你可能想做这样的事情:
HWND hTopmostWnd = hWnd;
EnumWindows(FindTopmostWnd, (LPARAM)&hTopmostWnd);