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. 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM