getparent方法的一些陷阱、m_pParentWnd

在mfc中大家经常用GetParent方法获取父窗口指针,但你会发现有时候他返回的并不是你理解上的父窗口,为什么了? 这是变态的微软隐含的一些约束导致的。

子窗口是不可以作为Dialog的父窗体的,至少MFC中是不能。

// a popup window cannot be owned by a child window

while (hWnd != NULL && (::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)) hWnd = ::GetParent(hWnd);

如果想获得Dialog的父窗体,请使用Dialog::m_pParentWnd。

在文档视图模型中由于view是mainframe的子窗口,则以view作为父窗口的对话框等通过getparent获取的都是mainframe窗口。

以下CTestDlg构造的时候传递窗口对象的指针,当前传的窗口对象指针是CMyboleView对象的指针

记住一点:子窗体(例如View)不能作为对话框的父窗体

当前情况下,以下函数获得的类型如下所示,

①、要获得对话框构造的时候的“父窗体”,只能用m_pParentWnd;子窗体获得父窗体指针

②、GetParent获得父窗口的指针(对话框有点不同)

③、GetParentFrame获得CFrameWnd的指针,不管子窗体还是对话框

④、GetParentOwner获得最直接的父窗体或者自己,只要不是子窗体,默认是它的父窗体

⑤、GetOwner,查看msdn如下:

If the window has no owner, then a pointer to the parent window object is returned by default. Note that the relationship between the owner and the owned differs from the parent-child aspect in several important aspects. For example, a window with a parent is confined to its parent window's client area. Owned windows can be drawn at any location on the desktop.

The ownership concept of this function is different from the ownership concept of GetWindow. 

 

转载于:https://www.cnblogs.com/develop-me/p/5744773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值