Owner owned relationship and parent child relationship in Windows system

Posted on

I. concept

  • In Windows system, each window object corresponds to a data structure, forming a list list list. The window manager of the system obtains window information and manages each window through the list. There are four pieces of data in this data structure to build a list, namelychild、sibling、parent、ownerThe four domain.
  • So we can see that there are two relationships between windows:Owner owned relationshipandParent child relationship。 The former is called the owning / being owned relationship, and the latter is called the parent-child relationship.
  • The relationship between windows determines the external performance of windows. Such as display, destruction, etc.
  • If the owner domain of a window’s data is not null, it establishes an owner owned relationship with the window, which determines:

    • The window owned is always displayed in front of the window owning it;
    • When the owner window is minimized, all its windows will be hidden;
    • When the owner window is destroyed, all the windows it owns are destroyed.
  • Be careful!Minimizing the owner window does not affect the visible state of the window it owns
  • If the parent domain of a window is not null, a parent child relationship is established between the window and the window. Father son relationship determines:

    • The display position of the window at the top of the screen.The parent window provides a coordinate system to locate the child window. A child window can only be displayed in the client area of its parent window, and the rest will be trimmed.
    • When the parent window is minimized, all its child windows are also hidden.
    • When the parent window is destroyed, all its child windows will be destroyed.
  • Be careful! Minimizing the parent window does not affect the visible state of the child window. The child window will be minimized as the parent window is minimized,But its WS? Visible property does not change
  • Windows system uses two kinds of relations to manage windows more flexibly. The parent window restricts the display area of the child window, while the owner window does not restrict the display area of the owned window. This is their biggest difference.
  • In addition, the message delivery between windows is also related to the window relationship. Usually, a window will send its own notification message to its parent window, but not all of them, so the message delivery between windows can also be flexibly controlled.

2、 Description and limitation of window type

  • Console window。 This is the first window created by the system. You can think of it as the owner and parent of all windows of type WS? Overlapped. When the system is initialized, it first creates a console window that covers the entire screen. All other windows are displayed on top of this console window. The first window list used by the window manager is this console. Its next window is called top level. Top level window refers to all windows that are not child windows, do not have a parent window, or the parent window is a desktop window. They do not have the ws_child attribute.
  • Ws? Overlapped typeCan be displayed anywhere on the screen. Its owner window is the console. Overlapped window belongs to the top-level window, which is generally the main window of the application. No matter whether the properties of WS ﹣ caption and WS ﹣ border are given or not, such windows have title blocks and borders after creation. All overlapped windows have the WS? Clipsiblings property. When the system shuts down, it will destroy all windows of type overlapped.
  • Ws? Popup typeThe windows of can be displayed anywhere on the screen. Generally, there is no parent window, but if setparent is explicitly called, such windows can also have a parent window.Similar to a window of type WS ﹣ overlapped, the owner of a window of type WS ﹣ popup is given by setting the hWndParent parameter in the CreateWindow or CreateWindowEx function, if hWndParent is not a child window, the window becomes the owner of the new pop-up window. Otherwise, the system looks up from the parent window of hWndParent until it finds the first non child window, and takes it as the owner of the pop-up window. In this case, the parameter hWndParent is saved in the parent domain of the new window, and its owner window handle is saved in the owner domain. When the owner window is destroyed, the system automatically destroys the pop-up window.
  • The windows of WS pop-up type are also top-level windows. The main difference between WS pop-up and WS overlapped windows is that WS pop-up windows do not need to have title bars or borders.
  • The owner window can only be a window of type WS ﹣ overlapped or WS ﹣ popup, the child window cannot be an owner window, and the owner window can also be owned.
  • By default, dialog boxes and message boxes belong to the owned window. Unless the ws_child attribute is explicitly given when they are created (for example, when dialog boxes are embedded in dialog boxes), the system is responsible for assigning them the owner window. It should be noted that once a window of type owned is created, its ownership can no longer be changed, because Win32 does not provide a way to change the window owner. Moreover, in Win32, due to the existence of multiple threads, it is necessary to ensure that the parent child window or the owner owned window belong to the same thread.
  • Ws? Child typeThe parent window of a window of is its owner window. The parent window of a child window is also specified in the CreateWindow function with the hWndParent parameter. A child window can only be displayed in the client area of the parent window and destroyed with the parent window.The child window must have a parent window, which is the main difference between it and the WS ﹣ overlapped and WS ﹣ popup windows.The parent window can be a top-level window or another child window.

3、 Explanation of several related functions

  • Get the owner window GetWindow (hWnd, GW owner) always returns the owner of the window. For a child window, the function returns null because the windows system does not maintain the owner information for the child window.
  • Get / set the parent window, the Win32 API gives the functionGetParent and setparent。 For setparent, MSDN indicates that the parent-child window must be in the same process. However, because the window handle is globally unique in the system, it can be called successfully when it does not belong to the same process, but the consequence is unknown. The return value of GetParent is complex. For windows of type ws_overlapped, it returns 0. For windows of type ws_child, it returns its parent window. For windows of type ws_popup, it returns its owner window.
  • Getwindowword (hWnd, gww? HWndParent) returns the parent window of a window, or its owner if not.
  • The showownedpopups (hWnd, fshow) function can display or hide all the windows owned by the owner window together.

Leave a Reply

Your email address will not be published.