获取父窗口的正确方法 [英] The correct way of getting the parent window

查看:63
本文介绍了获取父窗口的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN 关于 GetParent 函数的说明如下:

The MSDN says the following about the GetParent function:

获取父窗口而不是所有者,而不是使用 GetParent,而是使用 GetAncestorGA_PARENT 标志.

To obtain the parent window and not the owner, instead of using GetParent, use GetAncestor with the GA_PARENT flag.

但是当为没有父窗口的窗口调用GetAncestor(hWnd, GA_PARENT);时,它返回桌面窗口,而GetParent返回NULL.

But when calling GetAncestor(hWnd, GA_PARENT); for a window that doesn't have a parent, it returns the desktop window, while GetParent returns NULL.

那么获取父母(而不是所有者)并在没有时获取 NULL 的正确方法是什么?

So what is the correct way of getting a parent (and not the owner), and getting NULL if there are none?

当然我可以检查 GetAncestor 是否返回桌面窗口,但这对我来说似乎是一个黑客.

Of course I could check whether GetAncestor returns the desktop window, but that seems like a hack to me.

推荐答案

这是我的想法:

//
// Returns the real parent window
// Same as GetParent(), but doesn't return the owner
//
HWND GetRealParent(HWND hWnd)
{
    HWND hParent;

    hParent = GetAncestor(hWnd, GA_PARENT);
    if(!hParent || hParent == GetDesktopWindow())
        return NULL;

    return hParent;
}

这篇关于获取父窗口的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆