先说明一下两个窗口是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得到。