Add toolbar and RepositionBar function description in the VC dialog box

Source: Internet
Author: User

1. Add the toolbar resource ID as IDR_TOOLBAR.
2. Add the following to the class definition in the dialog box:
CToolBar m_ToolBar;
3. Add the following code in OnInitDialog or other appropriate message responses: (function can view MSDN)

M_ToolBar.Create (this); // create the toolbar m_ToolBar.LoadToolBar (IDR_TOOLBAR); // load the toolbar // obtain the control bar size. CRect rect; CRect rectNow; GetClientRect (rect); RepositionBars (AFX_IDW_CONTROLBAR_FIRST, limit, 0, reposQuery, rectNow); // place the control bar in CPoint ptOffset (rectNow. left-rect.left, rectNow. top-rect.top); CRect rcChild; CWnd * pwndChild = GetWindow (GW_CHILD); while (pwndChild) {pwndChild-> GetWindowRect (rcChild); ScreenToClient (rcChild); rcChild. offsetRect (ptOffset); pwndChild-> MoveWindow (rcChild, FALSE); pwndChild = pwndChild-> GetNextWindow ();} // adjust the dialog box size CRect rcWindow; GetWindowRect (rcWindow); rcWindow. right + = rect. width ()-rectNow. width (); rcWindow. bottom + = rect. height ()-rectNow. height (); MoveWindow (rcWindow, FALSE); // control bar positioning RepositionBars (AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0); // center the center of the box in CenterWindow ();

 

4. manually add processing functions
Afx_msg void OnBtnXXX (); // message response function declaration
ON_COMMAND (ID_BTN_XXX/* tool button ID */, OnBtnXXX/* function name */) // message ing
Void CXXXDlg: OnBtnXXX () {}// Message Processing Function

Detailed analysis of MFC Window Location Management and examples in the window client area of a program written in MFC, there may be several subwindows (Windows with WM_CHILD style ). The top is the toolbar, the middle is the View window, and the bottom is the status bar. The three windows coexist in the customer area of the framework and do not overlap with each other. The size of the main frame window has changed. Other subwindows can adjust their size in time to keep their positions unchanged. For example, the status bar window can always be at the bottom of the main frame client area, and its width is always the same as that of the main frame customer zone. The toolbar window can always be docked on one side of the main frame, and its width or height can always be the same as the width or height of the main frame customer area. The View window can always fill the remaining space in the main frame customer area.

If we derive a window class from the CWnd class and generate a window, several subwindows will be generated in its customer area. We want to make these subwindows arranged in a regular and non-overlapping manner, when the size of the parent window changes, the size and position of each child window can be adjusted in a timely manner, so that the proportional relationship between the positions and sizes of each child window remains unchanged. When one or several subwindows are moved, other subwindows can give up their positions in time. Of course, we can use the Management window function in api functions to compile our own method for managing subwindows. However, if you have a toolbar, Status Bar, and other subwindows in the parent window, can the subwindows you added work well with the subwindows provided by the mfc? How do you ensure that your subwindow does not overwrite the toolbar that can be docked everywhere? How can you know when the toolbar and status bar disappear, so that you can adjust your size in time to overwrite the space occupied by the toolbar and status bar? There is also a view in the customer zone of the window based on the document view framework. Can the child windows that you add hard on your own compete with the View window?

Therefore, you must understand the methods for managing the customer zone of the mfc window. In fact, the method for managing the client zone of the mfc window is very simple: the parent window calls a function, and the subwindow responds to a message, so much.

CWnd: RepositionBars function and WM_SIZEPARENT message

First, let's briefly describe the process of allocating the customer space for the Child window in the mfc window: this process is completed by the parent window and the Child Window. The parent window first provides a region in the customer zone, which is called the start zone. Then, call a function. In this function, the parent window submits this area to the first subwindow through a message. The subwindow determines the size of the subwindow that you want to occupy, then, you can drag out the occupied part in the available area so that the available area is split. The parent window then submits the remaining available areas to the second child window through the same message, and the second child window switches out one piece as needed. In this way, each sub-window is split into one that you need. The remaining zones are used in the last sub-window. It can be seen that, except for the last subwindow, all other subwindows have to have their own algorithms in the message response function to determine the size of each subwindow in the available area, this algorithm is not needed because the last sub-window has no choice.

Of course, the initial available area is a rectangle, and the remaining available area after each cut is still a rectangle, which cannot be in other shapes.

For example, in a typical single-document program, the parent window is the main frame window derived from CFrameWnd, And the last sub-window is the View window. If CSplitterWnd is used to generate separation bars, the last sub-window is the window with separators. Other subwindows are toolbar windows, status bar windows, and other control windows.

In a typical multi-Document Interface Program, the parent window is the main frame window, And the last child window is the window that covers the main window client area. The background is black-gray and has the child frame window containing the document, this is a window with a predefined window class. Its window class name is "MDIClient ". If CSplitterWnd is used to generate a separator, the last subwindow is the one with the separator. Other windows are toolbar windows, status bar windows, and other control windows.

The function and message are: CWnd: RepositionBars () and WM_SIZEPARENT. This message is customized by mfc, not by windows.

This function and message are briefly described.

1. Function CWnd: RepositionBars ()

This function is not a virtual function, so you cannot compile your own version by overwriting In the derived class. You can only understand its functions for flexible use.

To put it simply, this function places the available customer zone information in the message parameters of the message WM_SIZEPARENT, and then enumerate all the subwindows in this window, send this message to each subwindow (removing a specific subwindow is equivalent to the last subwindow mentioned above, each subwindow that responds to this message will split the available customer zones. At last, adjust the size and position of the specific sub-window to the available area at the end.

2. Message WM_SIZEPARENT

Each subwindow to participate in the distribution of customer areas must respond to this message, unless this subwindow is the specific subwindow.

There are at least two things to do in the subwindow to respond to this message: 1. Switch the available parent window customer partition to the one occupied by the customer. 2. According to the Message Parameter instructions, adjust the size and position of the message to fit in the area occupied by the message or do not adjust it.

The following describes in detail the function CWnd: RepositionBars () and message WM_SIZEPARENT.

1. Function CWnd: RepositionBars () void RepositionBars (UINT nIDFirst, UINT nIDLast, UINT second, UINT nFlag = CWnd: reposDefault, LPRECT second = NULL, LPCRECT lpRectClient = NULL, BOOL bStretch = TRUE );

There are many parameters, but they are quite understandable.

(1) nIDFirst and nIDLast

The id range of the subwindow that participates in allocating the parent window customer area.

Each WM_CHILD window has an id, which is specified during window creation. The sixth parameter of the function CWnd: Create () is the id. The HMENU type parameter in the CreateWindow and CreateWindow Wex functions. When the window style contains WM_CHILD, it is not the menu handle, but the id of the window.

The nIDFirst and nIDLast parameters indicate that, if the id value of a subwindow is greater than or equal to nIDFirst and less than or equal to nIDLast, WM_SIZEPARENT messages will be sent to this subwindow in this function, this subwindow can be used to allocate the customer area in the parent window.

(2) nIDLeftOver

As mentioned above, there is a specific subwindow that does not respond to the WM_SIZEPARENT message. Only when other sub-windows are allocated is used up will the remaining sub-windows in the parent window be retrieved. NIDLeftOver is the id of this subwindow. It must be greater than or equal to nIDFirst and less than or equal to nIDLast.

(3) lpRectClient

This is a pointer to the RECT structure data. This RECT structure stores the initial available areas of the parent window customer zone. As the function sends the WM_SIZEPARENT message to each subwindow in sequence, each subwindow that responds to the message will remove the portion occupied by it. The last part is the area that the subwindow with id nIDLeftOver will occupy. This parameter can be NULL, and the initial zone is the entire parent window customer zone.

(4) nFlag and lpRectParam

It is better to put these two parameters together. NFlag is the Function Identifier of the function. It can have three values: reposDefault, reposQuery, and reposExtra.

When nFlag is equal to reposDefault, The RepositionBars function sends WM_SIZEPARENT messages to subwindows whose IDs are between nIDFirst and nIDLast and are not equal to nIDLeftOver, each subwindow that responds to the Message removes the part occupied by it from the structure indicated by lpRectClient, and adjusts its size and position to the size of the region occupied by it, finally, the RepositionBars function also adjusts the size and position of the subwindow whose id is nIDLeftOver to the available area left by other subwindows, so that the subwindow completely overwrites the last available area. In this case, lpRectParam is not required. It can be NULL.

When nFlag is equal to reposQuery, The RepositionBars function sends WM_SIZEPARENT messages to subwindows whose IDs are between nIDFirst and nIDLast and are not equal to nIDLeftOver, each subwindow that responds to this message is segmented from the structure indicated by lpRectClient, but they do not adjust their size and position, finally, the RepositionBars function does not adjust the size and position of the subwindow whose id is nIDLeftOver. Instead, it performs an action based on the bStretch value. If bStretch is TRUE, the RepositionBars function copies the last available regions to the RECT structure pointed to by lpRectParam. If bStretch is FALSE, then the RepositionBars function puts the height and width of the available areas occupied by all other sub-Windows (this value makes sense only when all sub-windows are tightly arranged to form a large rectangle) copy to the bottom of the RECT structure pointed to by lpRectParam
And right members, the top and left members are set to zero. The purpose of calling RepositionBars with this nFlag value is not to rearrange subwindows, but to see how many subwindows will occupy if subwindows are rearranged, the location of the last available area and so on.

When nFlag is equal to reposExtra, the function is similar to nFlag when it is equal to reposDefault. In this case, lpRectParam is used. As mentioned above, when nFlag is equal to reposDefault, The RepositionBars function will adjust the size and position of the subwindow with the id of nIDLeftOver to the available area left by other subwindows, make this sub-window completely overwrite the last available area. When nFlag is equal to reposExtra, RepositionBars must use
LpRectParam to correct the last available area. If lpRect points to the final zone, the correction is as follows:

LpRect-> top + = lpRectParam-> top;
Lprect-> left + = lpRectParam-> left;
LpRect-> right-= lpRectParam-> right;
LpRect-> bottom-= lpRectParam-> bottom;

With this correction, we can leave the remaining available areas empty for use instead of the sub-windows with the id of nIDLeftOver.
(5) bStretch

The role of this parameter has been mentioned above. It is mainly provided for subwindows that respond to WM_SIZEPARENT messages. when determining the number of subwindows, such as the toolbar and status bar, you can determine how much space you want to move from the free space in the customer zone of the parent window, this parameter is also a basis for judgment. For details, see the OnSizeParent function of the toolbar and status bar responding to WM_SIZEPARENT ().

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.