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. 

 

posted @ 2016-08-06 19:59  发展才是硬道理  阅读(3393)  评论(0编辑  收藏  举报