Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 window Owner vs window Parent?

In Win32 programming, what is the difference between a window's parent and a window's owner? I thought I had it figured out, then I came across this code:

SetWindowLong(handle, GWL_HWNDPARENT, foo);

This actually sets the window's owner, not the parent - despite the GWL_HWNDPARENT being used. Are the terms parent/owner interchangeable, or is there actually a difference?

like image 253
Jon Tackabury Avatar asked Feb 03 '09 16:02

Jon Tackabury


People also ask

What is window owner?

Ownership is established when the Owner property of a window (the owned window) is set with a reference to another window (the owner window). Once this relationship is established, the following behaviors are exhibited: If an owner window is minimized, all its owned windows are minimized as well.

What are parent and child windows?

A window that opens inside a parent window is a child window. The action taken on the parent window reflects on the child window as well. For example, if you close the parent window, the child window closes too.

How do I find parent windows?

To obtain a window's owner window, instead of using GetParent, use GetWindow with the GW_OWNER flag. To obtain the parent window and not the owner, instead of using GetParent, use GetAncestor with the GA_PARENT flag.


3 Answers

Ownership is a relationship between two top level windows while Parent is a relationship between a top level and a WS_CHILD, or a WS_CHILD and another WS_CHILD.

The parent of a button is the form it is on, while a message box is owned by the form that showed it.

Read this article from Microsoft Win32 Window Hierarchy and Styles to get a much clearer understanding of Ownership, Parenting, ZOrder, SetWindowLong, GetWindow and all the other nasty bits of the Win32 api for creating window relationships.

EDIT: Looks like Microsoft took down that content, here is another reasonable summary of Ownership / Parenting.

like image 65
Maurice Flanagan Avatar answered Sep 21 '22 13:09

Maurice Flanagan


Owner is the Window* responsible for a control or dialog (for example, responsible for creating/destroying the window).

Parent is the next-senior window* to a control or dialog in the window chain, but isn't actually responsible for it (doesn't necessarily care about its lifecycle, etc). A window's parent can also be its owner.

*Window vs window: Window is an actual window displayed on the screen; window is any object with a HWND (includes buttons, panels, etc).

like image 30
TheSmurf Avatar answered Sep 19 '22 13:09

TheSmurf


Chen's blog post is the one to read. The key point for me is that the WS_CHILD style must be used on the child window. You can attempt to create a child window and pass the parent handle in to CreateWindow( ), but if you don't have the WS_CHILD style set the two windows will have the owner relationship, not the parent/child relationship.

like image 44
osullivj Avatar answered Sep 17 '22 13:09

osullivj