MFC window, parent window parentwindow, owner window Ownerwindow difference

Source: Internet
Author: User
Tags win32 window

MFC window, parent window parentwindow, owner window Ownerwindow difference

1. Understanding the relationship between Windows 2. How to set (create) a different window

One

Parent: Creator, Owner: Owners

Xiao Yu's parents gave birth to Jade, raised to 8 years old, sold to Jia Fu when the girl
Xiao Yu's parents are the parent, Jia is owner

Two

1.pop-up window: A pop-up window is a window that must have the Ws_popup property, the pop-up window can only be a top-level window, not a child window, pop-up windows for dialog boxes and
can also be used in the application's main window, and the pop-up window may not have a title bar

2.Overlapped window: An overlapping window is a window that must have the Ws_overlapped property, i.e. an overlapping window must have a border, title bar, and customer area. Overlapping windows
Can be a child window, or it can be a top-level window, and overlapping windows are typically used for an application's main window

3.top-level window: Is the window without the Ws_child property, all top-level's parent window is the desktop window. Not all top-level windows are in the system's taskbar
Display, only the top-level window with the owner window is null to display the title of the window in the taskbar

4.Child window: A window with a parent window is called a child window, the child window must have the Ws_child property, and the child window can have child windows, the child window can be an overlapping window, but cannot be
A pop-up window. A child window can only be included in the client area of the parent window, cannot be moved out of the client area of the parent window, and the child window cannot be the active window, nor can it
Displays the caption of the child window on the taskbar. Both the maximized and minimized child windows are displayed in the parent window, and the rect specified when a child window is created is a parent window
The upper-left corner of the customer area of the port is the origin point. When a non-child window is created, the rect specified is based on the screen as the origin of the coordinates. A child window can have a system menu, but not a dish
Single column. The child window is destroyed before the parent window is destroyed, hidden before the parent window is hidden, and displayed after the parent window is displayed.

5.Desktop window: Is the desktop, the Desktop window is a special window, he is neither a child window, nor an overlapping window, nor a pop-up window.

6.Parent window: A window with a child window is called a parent window, and the child window to which the parent window is destroyed is automatically destroyed. When the parent window is minimized, its child windows are also minimized, but not hidden
The parent window is restored with its child window restored.

7.Owner window: Only overlapping windows and pop-up windows can be owner window, child window cannot be owner window, owner window is destroyed, all of his owned Windows will be destroyed automatically,
When the owner window is hidden, all of his owned windows will not be hidden. But when owner minimization is his owned window will be hidden. All owned windows of an owner window are displayed in front of the owner window, not behind them.

8.Owned window: A owned window is always in Z-order order usually before his owner window, owned window of life can be controlled by his owner window, owned window of the display
The display is not limited to his owner window area, a owned window cannot be changed after the creation of his owner window, child windows, popups, overlapping windows can be done owned
Window.

9. Z-order of the window:

10.message-only window: The message window cannot be displayed, cannot accept keyboard and mouse messages, will not receive broadcast messages, and the message window will not be enumerated, will not appear in the window
In Z-order order. The other is the same as in the General window. can receive and send messages. Overlapping, popup, child window can be a message window

11.background window: Non-foreground window is called background window

12.foreground window: If the user is on a window created by a thread, then this thread is called the foreground thread, and this window is called the foreground window, at which point the other threads are called the background
Thread, the window of a background thread is called a background window, and the priority of the foreground thread will have to be a little higher priority for the background thread. When a window becomes a foreground window
, this window also becomes the active window. The system has a RIT thread to maintain the hardware input queue Shiq, each GUI thread maintains a virtual input
Queue, and in one time only one thread's virtual input queue is associated with Shiq, the thread that is associated with Shiq is called the foreground thread.


13.active window: The active window is a top-level window that the user is working on, and the active window is generally placed at the top of the z order and the title bar color of the window is highlighted. Only the top
The layer window can be used as the active window, and when the user is working on a child window, a top-level parent window that the child window belongs to becomes the active window, at a time
There is only one top-level window in the active window, and the active window is the window that is currently receiving keyboard and mouse input.

14.disabled window: A window that cannot respond to keyboard and mouse input
15.visible window: Window not visible

Three

Determines the window's parent window and owner window


When calling the CreateWindow (Ex) function to establish a window, the parent window and owner window of a window are determined according to hWndParent and Dwstyle, and the following are distinguished according to whether hwndparent is null:

1.hWndParent = = NULL (must be a non-ws_child window to hwndparent null)
For dwstyle with Ws_child, hwndparent cannot be null, so the main thing is to create overlapping windows and pop-ups, in which case the parent window of the created window will be the system's desktop window, and the window's owner window is null.

2. hwndparent! = NULL
For dwstyle with Ws_child, hwndparent is the parent window of the new window, and the owner window of the new window is null; For overlapping windows or pop-ups without ws_child, the new window's parent window will be null, and hwndparent is the owner window of the new window, and only hwndparent is an overlapping and pop-up window that qualifies as an owner window. Therefore, if hwndparent is a desktop window, the owner window of the new window is null, and if hwndparent is a child window, the owner window of the new window is the top-level parent window of the hWndParent window.

Four

In the Dreeatewindow method, the parent window or owner window has been determined.
The parent window of the child window must not be empty, but all of its people must be empty.
Overlapping windows and pop-ups if hwndparent is not empty, it is its owner window, and their parent window must be empty.

As a result, the child window must have a parent window, and there must be no owner window. Overlapping, pop-up windows may have a window, generally no parent window.

I. Concepts and differences
     in Windows systems, each window object should have a data structure, form a list linked list. The window manager of the system uses this list to retrieve window information and manage each window. In this data structure, there are four data to build the list, that is, child, sibling, parent, owner four domains.
     so we can see that there are two types of relationships between Windows: owner-owned relationships and Parent-child relationship. The former is called the owning/being possessed relationship, the latter is called the parent/child relationship. In this text, I put the owner window called the owners window. In other words, a window may be owned by a different window (owner) While there is a parent window, or it can have its own child window. In the CWnd Class of MFC, the owner window is saved in the M_hwndowner member variable, and the parent window is saved in M_hparent, but the two values do not necessarily correspond to the values in the Window object's data structure. The relationship between
   
     Windows determines the appearance of the window. such as display, destruction and so on.

     if the owner domain of a window data is not NULL, it establishes a owner-owned relationship with the window. The owning relationship determines:
     (1) The owned window is always displayed in front of the window that owns it;
      (2) When the owner window is minimized, the window it owns will be hidden;
     (3) When the owner window is destroyed, the window it owns will be destroyed.
     it is important to note that hiding the owner window does not affect the visible state of the window it owns. For example: If window A has window B and Window B has window C, window B is hidden when window A is minimized, but window C is still visible.


If the parent field of a window is not NULL, then it establishes a parent-child relationship with the window. The father and son decided:
(1) The display position of the window above the screen. The parent window provides the coordinate system used to locate the child window, and a child window can only be displayed in the client area of its parent window, and the outside portion will be trimmed. This reduction rule determines that if the parent window is not visible, the child window is definitely not visible. If the parent window is moved outside the screen, the child window is the same.
(2) When the parent window is hidden, all its child windows are also hidden.
(3) When the parent window is destroyed, the child window that it owns will be destroyed.
Watch out! The minimized parent window does not affect the visible state of the child window, and the child window is minimized with the parent window, but its Ws_visible property does not change.

Why do Windows systems use two relationships? This is for a more flexible management window. For example, the drop-down list box (combobox) of a combo box can go beyond the client area of the parent window of the combo box, so it is useful to display, so the system creates the list box as a subwindow of the console window (the desktop Windows). Its parent window hwndparent is null so that the list box's display area is limited to the entire screen, but the list box owner is the first non-subwindow ancestor of the combo box (such as a dialog box), and the list box is automatically destroyed when its owner window is destroyed.

In addition, the delivery of messages between windows is also related to window relationships, and usually a window sends its own notification message to its parent window, but not all, for example, CToolBar sends a notification message to its owner window instead of the parent window. This allows the toolbar to be used as a subwindow for a window (such as an OLE container program window) and to send messages to another window (such as the in-place frame window). Microsoft does not explicitly indicate to whom a certain type of window is sent, whether it is a parent window or an owner window. Also, in the case of field (in-place) editing, when a server window is active or invalid, the window's own subwindow is automatically hidden or displayed, which is also achieved by calling the SetOwner function directly.


Ii. description and limitations of window types

(1) Console Windows (Desktop window). This is the first window created by the system. You can think of it as the owner and parent window of all ws_overlapped type Windows. In his article "Win32 window Hierarchy and Styles," Kyle Marsh points out that when the system initializes, it first creates a console window that covers the entire screen. All other windows are displayed on this console window. Window Manager The window that is used in the list is the first one in this console. The next layer of the window is called the top-level window (top-level), and the top-level window refers to all non-child, no-parent windows, or windows that are desktop windows, and they do not have a ws_child property.

(2) ws_overlapped type of window can be displayed anywhere on the screen. Their owner window is the console.

windows of the Overlapped type belong to the top-level window, typically as the main window of the application. This type of window is created with a title bar and a border, regardless of whether the ws_caption or Ws_border attribute is given. The overlapped window can have other top-level windows or be owned by other top-level windows. All overlapped windows have ws_clipsiblings properties. The system can automatically set the size and initial position of the overlapped window.

when the system shut down, it will destroy all windows of the overlapped type.

(3) Ws_popup types of Windows can be displayed anywhere on the screen, they generally do not have a parent window, but if SetParent is explicitly called, such windows can also have a parent window.

The owner of a window of type ws_popup is given by setting the hWndParent parameter in the CreateWindow function, if hwndparent is not a child window, the window becomes the owner of this new pop-up window, otherwise, The system is looked up from the parent window of the hwndparent until the first non-child window is found, as the owner of the popup window. When the owner window is destroyed, the system automatically destroys the popup window.

pop-up types of Windows also belong to the top-level window, and the main difference between it and the overlapped window is that pop-up windows do not need to have a title bar, or have a border. Pop-up can have other top-level windows or be owned. All pop-up windows also have the Ws_clipsiblings property.

(4) The owner window can only be a window of the overlapped or pop-up type, and the child window cannot be the owner window, that is, the child window cannot have other windows.

overlapped or pop-up types of Windows can also be owned while owning other windows.

when you use CreateWindowEx to create a window of type ws_overlapped or ws_popup, you can give a handle to its owner window in the hwndparent parameter. If hWndParent gives a child-type window handle, the system automatically assigns ownership of the newly created window to the top-level parent window of that subwindow. 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.

(5) By default, dialog boxes and message boxes belong to the owned window, unless the Ws_child attribute is explicitly given when they are created (such as when a dialog box is embedded in dialogs)
Otherwise, the system is responsible for assigning them the owner window. It is important to note that once you create a window of type owned, you can no longer change all of its relationships, because WIN32 does not provide a way to change the window owner.

and in Win32, due to the existence of multiple threads, it is important to ensure that the parent-child window or owner/owned window belongs to a thread.

(6) For a window of type ws_child, its parent window is its owner window. The parent window of a child window is also specified in the CreateWindow function with the hwndparent parameter. Child windows 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 overlapped and pop-up windows. The parent window can be a top-level window, or it can be a different child window.


Iii. description of several related functions

(1) Get/Set owner window
The Win32 API provides a function GetWindow function (Gw_owner flag) to get the owner window handle of a window.
GetWindow (hWnd, Gw_owner) always returns the owner of the window (owner). For child windows, the function returns NULL because their parent window is equivalent to the owner (note that is "equivalent"). Because the Windows system does not maintain owner information for child windows.

in MFC, the owner window pointer is obtained by using the following function:
_afxwin_inline cwnd* cwnd::getowner () const
{return M_hwndowner! = NULL? Cwnd::fromhandle (M_hwndowner): GetParent (); }
from the above code we can see that it returns the value and GetWindow returns the difference, if the current window does not have owner, then will return its parent window pointer.

However, Windows does not provide a way to change the window owner. MFC provides a way to change the owner:
_afxwin_inline void Cwnd::setowner (cwnd* pownerwnd)
{M_hwndowner = Pownerwnd! = NULL? pownerwnd->m_hwnd:null;}

In addition, MFC also provides Cwnd::getsafeowner (cwnd* pparent, hwnd* pwndtop), a function that can be used to get the parameter pparent the first non-child property of the parent window pointer. If this parameter is NULL, it returns the main window of the current thread (obtained by AfxGetMainWnd). The framework often uses this function to find the owner window of a dialog box or property page.

(2) Get/Set Parent window

the WIN32 API gives the functions GetParent and setparent. MFC also completely encapsulates these two functions:

_afxwin_inline cwnd* cwnd::setparent (cwnd* pwndnewparent)
{ASSERT (:: IsWindow (M_hwnd)); return Cwnd::fromhandle (:: SetParent (M_hwnd,
Pwndnewparent->getsafehwnd ())); }

_afxwin_inline cwnd* cwnd::getparent () const
{ASSERT (:: IsWindow (M_hwnd)); return Cwnd::fromhandle (:: GetParent (M_hwnd));}

for Setparent,msdn It is stated that the parent-child window must be the same process. However, because the window handle is globally unique and is not part of the same process, it can be called successfully, but the consequences are unknown.
The return value of GetParent is more complex, for a window of type overlapped, it returns 0, for Ws_child type, it returns its parent window, for the Ws_popup type, it returns its owner window, You should use the Getwindowword (gww_hwndparent) function if you want the hwndparent parameter that was passed in when you created it.

(3) Getwindowword (hWnd, Gww_hwndparent) returns the parent window of a window, if not, returns its owner.

(4) As mentioned above, when an owner window is minimized, the system automatically hides the Windows it owns. When the owner window is restored, the system automatically displays the Windows it owns. In both cases, the system sends a (send) Wm_showwindow message to the owning window. At some point, we may need to hide the owned window, but do not want to minimize its owner window, this time, can be implemented through the Showownedpopups function, which sets or removes the Ws_visible property of the window owned by the current window, and then sends Wm_ The ShowWindow Message Update window is displayed.

MFC window, parent window parentwindow, owner window Ownerwindow difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.