先說明一下兩個窗口是Parent&Child關系或是Owner&Owned關系會對他們有些什麼影響(摘自MSDN)
Parent&Child
If an application creates a child window that is larger than the parent window or positions a child window so that some or all of the child window extends beyond the borders of the parent, the system clips the child window
Parent Window
Child Window
Destroyed
Destroyed before the parent window is destroyed.
Hidden
Hidden before the parent window is hidden. A child window is visible only when the parent window is visible.
Moved
Moved with the parent window''s client area. The child window is responsible for painting its clIEnt area after the move.
Shown
Shown after the parent window is shown.
Owner&Owned
An owned window is always above its owner in the z-order.
The system automatically destroys an owned window when its owner is destroyed.
An owned window is hidden when its owner is minimized.
假設有hwnd1和hwnd2兩個窗口,如果他們要滿足Parent&Child關系,即hwnd2是hwnd1的子窗口,在創建Child的時候必須指定hwnd2的WS_CHILD屬性。然後要麼在創建hwnd2到時候指定其parent為hwnd1,或者在創建好hwnd2 後,通過SetParent指定hwnd1為其父窗口。
如果hwnd2沒有WS_CHILD屬性,僅僅通過調用SetParent指定其父窗口為hwnd1,那麼只會將hwnd2成為hwnd1事實上的子窗口,但並不改變他們名義上的關系(甚至都不會讓hwnd1成為hwnd2的Owner),即hwnd2的父窗口仍可能為NULL,而且hwnd2仍是Top-Level window。
如果hwnd2沒有WS_CHILD屬性,但CreateWindow的時候指定了其Parent參數為hwnd1,那麼hwnd1將會成為hwnd2的Owner window,他們的關系就變成了Owner&&Owned關系。得到hwnd2的Owner,可以通過用GW_OWNER參數調用GetWindow得到。