当前位置:网站首页>Win32 child window parent window window owner

Win32 child window parent window window owner

2022-06-10 19:07:00 Plug in development


   stay windows In the system , Each window object has a corresponding data structure , To form a list Linked list . The window manager of the system uses this list To get window information and manage each window . There are four pieces of data in this data structure to build list, namely child、sibling、parent、owner Four domains .

1. Window relations

   There are two kinds of relationships between windows :owner-owned Relationship and parent-child Relationship . The former is called having / Be owned by , The latter is called the father / Child relationship . In this article , call owner The window is the owner window . let me put it another way , A window has a parent window (parent) At the same time , It may also be owned by different windows (owner), You can also have your own sub window (child). stay MFC Of CWnd Class , The owner window is saved in m_hWndOwner In the member variable , The parent window is saved in m_hParent in , However, these two values do not necessarily correspond to the values in the window object data structure .
   If a window data owner Domain non NULL, Then it and the window establish owner-owned Relationship , Ownership determines :
  (1) The owned window is always displayed in front of the window that owns it ;
  (2) When the owner window is minimized , All the windows it has will be hidden ;
  (3) When the owner window is destroyed , All the windows it has will be destroyed .
   It should be noted that , Hiding the owner window does not affect the visibility of the windows it owns . such as : If the window A Have a window B, window B Have a window C, When the window A When minimized , window B Be hidden , But the window C Still visible .

   If a window parent Domain non NULL, Between it and the window parent-child Relationship . A parent-child relationship has the following characteristics :
  (1) The display position of the window on the screen . The parent window provides the coordinate system used to locate the child window , A child window can only be displayed in the client area of its parent window , The rest will be cut . This pruning rule determines if the parent window is not visible , The child window must not be visible . If the parent window is moved outside the screen , The same goes for child windows .
  (2) When the parent window is hidden , All its child windows are also hidden .
  (3) When the parent window is destroyed , All child windows it has will be destroyed .
   Be careful ! Minimizing the parent window does not affect the visibility of the child window , The child window is minimized with the parent window , But it's WS_VISIBLE Properties do not change .

2. Description and limitations of window types

  (1) Console window (desktop window). This is the earliest window created by the system . You can think of it as all WS_OVERLAPPED Type window owner and parent window .
  (2)WS_OVERLAPPED Type of window can be displayed anywhere on the screen . Their owner window is the console . When the system shuts down When , It will destroy all overlapped Type of window .
  (3)WS_POPUP Type of window can be displayed anywhere on the screen , They usually have no parent window , But if you explicitly call SetParent, This type of window can also have a parent window . WS_POPUP The owner of a window of type is in CreateWindow Function by setting hWndParent Parameter given , If hWndParent Not a sub window , Then the window will become the... Of the new pop-up window owner, otherwise , System from hWndParent Look up in the parent window of , Until you find the first non child window , Make it the of the pop-up window owner. When owner When the window is destroyed , The system automatically destroys this pop-up window .
  (4) The owner window (owner) Can only be overlapped perhaps pop-up Type of window , A child window cannot be an owner window , That is, child windows cannot have other windows .overlapped perhaps pop-up Windows of type have other windows at the same time , Can also be owned . In the use of CreateWindowEx establish WS_OVERLAPPED perhaps WS_POPUP Type window , Can be in hwndParent Parameter gives a handle to its owner window . If hwndParent What's given is a child Window handle of type , The system will automatically transfer the ownership of the newly created window to the top-level parent window of the child window . under these circumstances , Parameters hwndParent Saved in the new window parent domain , Its owner window handle is stored in owner domain .
  (5) By default , Dialog boxes and message boxes belong to owned window , Unless they are explicitly given when they are created WS_CHILD attribute ,( For example, the dialog box is embedded in the dialog box ) Otherwise, the system is responsible for assigning owner window . It should be noted that , Once created owned Type of window , Can no longer change all their relationships , because WIN32 There is no way to change the window owner . And in Win32 in , Due to the existence of multithreading , So pay attention to ensure that the parent-child window or owner/owned Windows must belong to the same thread .
  (6) about WS_CHILD Type of window , Its parent window is its owner window . The parent window of a child window is also in CreateWindow Function hWndParent Parameter specified . A child window can only be displayed in the client area of the parent window , And destroy it with the parent window .

3. Description of several related functions

  GetWindow(hWnd, GW_OWNER) Always return to the owner of the window (owner).
   about SetParent,msdn It explains that the parent-child window must be in the same process . But because the window handle is globally unique in the system , Not belonging to the same process , You can also successfully call , But the consequences are unknown .GetParent The return value of is complex , about overlapped Type of window , It returns 0, about WS_CHILD type , It returns to its parent window , about WS_POPUP type , It returns to its owner window , If you want to get the one you passed in when you created it hwndParent Parameters , Should use the GetWindowWord(GWW_HWNDPARENT) function .
  GetWindowWord(hWnd, GWW_HWNDPARENT) Returns the parent window of a window , without , Return its owner .

4. Author Q & A

   If you have any questions , Please leave a message .

原网站

版权声明
本文为[Plug in development]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101813416599.html