I. Introduction to the third part
Since its appearance as a universal control of Windows 95, tool bar and status bar have become very common. MFC supports floating toolbar to make them more popular. With the update of common controls, rebars (originally referred to as coollbar) provides another way to display toolbar. In Part 3, I will introduce how wtl supports these control bars and how to use them in your program.
2. tool bar and status bar of the Main Window
Cframeworkwimpl has three hwnd member variables initialized during window creation. We have seen m_hwndclient, which is the handle to fill the "View" window in the main window client area, now we have two more:
- M_hwndtoolbar: The Window handle of the toolbar or rebar.
- M_hwndstatusbar: Window handle of the status bar
Cframeworkwimpl supports only one tool bar, and does not support multiple-point dock tool bars like MFC. If you want to use multiple tool bars and do not want to modify the internal code of cframeworkwimpl, you need to use rebar. I will introduce them and demonstrate how to use the Application Wizard to add toolbar and rebar.
Cframeworkwimpl: onsize () The message response function calls updatelayout () and updatelayout () to do two things: locate all control bars and change the size of the View window to fill the entire customer zone. The actual work is completed by updatebarsposition (). updatelayout () only calls this function. The implementation code is quite simple. wm_size messages are sent to the toolbar and status bar. The default window processing process of these control bars positions them at the top or bottom of the main window.
When you tell the Application Wizard to add toolbar and status bar to your window, the wizard adds the code to create them in cmainframe: oncreate. Now let's take a look at the code, of course, to write another clock program.
3. The Wizard generates code for the toolbar and status bar
We will start a new project and ask the Wizard to create a toolbar and status bar for the main window. First, create a new project named wtlclock2. On the first page of the wizard, select SDI and select the "generate CPP file" check box:
On the second page, cancel the rebar so that the wizard only creates a common toolbar:
Copy the corresponding code from the second part of the program. The new program looks like this:
4. How to Create a toolbar and a status bar for cmainfracmainframe
In this example, the wizard adds more code to the cmainframe: oncreate () function, which is used to create a control bar and notify cupdateui to update the buttons on the toolbar.
Lresult cmainframe: oncreate (uint/* umsg */, wparam/* wparam */, <br/> lparam/* lparam */, bool &/* bhandled */) <br/>{< br/> createsimpletoolbar (); <br/> createsimplestatusbar (); </P> <p> m_hwndclient = m_view.create (...); </P> <p> //... </P> <p> // register object for message filtering and idle updates <br/> cmessageloop * ploop = _ module. getmessageloop (); <br/> atlassert (ploop! = NULL); <br/> ploop-> addmessagefilter (this); <br/> ploop-> addidlehandler (this); </P> <p> return 0; <br/>}
This is the beginning of the newly added code. The cframeworkwimpl: createsimpletoolbar () function uses the resource idr_mainframe to create a toolbar and assign its handle to m_hwndtoolbar. The following is the code of the createsimpletoolbar () function:
Bool cframeworkwimpl: createsimpletoolbar (<br/> uint nresourceid = 0, <br/> DWORD dwstyle = atl_simple_toolbar_style, <br/> uint nid = atl_idw_toolbar) <br/>{< br/> atlassert (! : Iswindow (m_hwndtoolbar); </P> <p> If (nresourceid = 0) <br/> nresourceid = T: getwndclassinfo (). m_ucommonresourceid; </P> <p> m_hwndtoolbar = T: createsimpletoolbarctrl (m_hwnd, nresourceid, true, <br/> dwstyle, NID); <br/> return (m_hwndtoolbar! = NULL); <br/>}
Parameters:
-
Nresourceid
-
The ID of the toolbar resource. If the default value 0 is used as the parameter, the program uses the declare_frame_wnd_class macro to specify the resource. Here, idr_mainframe is the code generated by the wizard.
-
Dwstyle
-
The type or style of the toolbar. The default value of atl_simple_toolbar_style is defined as tbstyle_tooltips. The combination of subwindows and visible styles makes a tooltip pop-up when you move the mouse over the button.
-
Nid
-
The window ID of the toolbar. The default value is usually used.
Createsimpletoolbar () first checks whether a toolbar has been created, and then calls the createsimpletoolbarctrl () function to create a toolbar control. The Toolbar Control handle returned by createsimpletoolbarctrl () is saved in m_hwndtoolbar. Createsimpletoolbarctrl () is used to read resources, create corresponding toolbar buttons, and return the toolbar window handle. This part of the code is quite long. I will not introduce it here. If you are interested in it, why do you find the code in atlframe. h.
The oncreate () function then calls the cframeworkwimpl: createsimplestatusbar () function. This function creates a status bar and stores the handle in m_hwndstatusbar. The following is the code of this function:
Bool cframeworkwimpl: createsimplestatusbar (<br/> uint ntextid = atl_ids_idlemessage, <br/> DWORD dwstyle =... sbars_sizegrip, <br/> uint nid = atl_idw_status_bar) <br/>{< br/> tchar sztext [128]; // Max text lentgth is 127 for status bars <br/> sztext [0] = 0; <br/>: loadstring (_ module. getresourceinstance (), ntextid, sztext, 128); <br/> return createsimplestatusbar (sztext, dwstyle, NID); <br/>}
The text displayed in the status bar is loaded from the string resource. The parameter of this function is:
-
Ntextid
-
The resource ID of the string displayed on the status bar. The string generated by the wizard for atl_ids_idlemessage is "ready ".
-
Dwstyle
-
The style of the status bar. The default value includes the sbars_sizegrip style, which makes the bottom right corner of the status bar display a flag for changing the window size.
-
Nid
-
Window ID of the status bar. The default value is usually used.
Createsimplestatusbar () calls another overload function to create a status bar:
Bool cframeworkwimpl: createsimplestatusbar (<br/> lpctstr lpstrtext, <br/> DWORD dwstyle =... sbars_sizegrip, <br/> uint nid = atl_idw_status_bar) <br/>{< br/> atlassert (! : Iswindow (m_hwndstatusbar); <br/> m_hwndstatusbar =: createstatuswindow (dwstyle, lpstrtext, m_hwnd, NID); <br/> return (m_hwndstatusbar! = NULL); <br/>}
This overload version first checks whether a status bar has been created, and then calls createstatuswindow () to create a status bar. the status bar handle is stored in m_hwndstatusbar.
5. Display and hide toolbar and status bar
The cmainframe class also has a view menu, which has two Commands: Display/hide toolbar and status bar. Their IDs are id_view_toolbar and id_view_status_bar. The cmainframe class has the response functions of these two commands, respectively displaying and hiding corresponding control bars. The following is the code of the onviewtoolbar () function:
Lresult cmainframe: onviewtoolbar (word/* wnotifycode */, word/* WID */, <br/> hwnd/* hwndctl */, bool &/* bhandled */) <br/>{< br/> bool bvisible =! : Iswindowvisible (m_hwndtoolbar); <br/>: showwindow (m_hwndtoolbar, bvisible? Sw_shownoactivate: sw_hide); <br/> uisetcheck (id_view_toolbar, bvisible); <br/> updatelayout (); <br/> return 0; <br/>}
These code flip control bar display status, corresponding flip view | check mark on the toolbar menu, then call updatelayout () to reposition the control bar and change the size of the View window.
Vi. inherent features of tool bar and status bar
The framework of MFC provides many good features, such as toolbar button prompts and menu item passing help. The corresponding functions of wtl are implemented in the cframeworkwimpl class. The following screen displays tool prompts and pass-through help.
The cframewindowimplbase class has two message-related functions to implement these functions. onmenuselect () processes wm_menuselect message elimination, and it looks for the character strings that pass the help like MFC: first, load the string resource with the same ID as the menu resource, search for/n characters in the string, and use the content before/N as the help content. The ontooltiptexta () and ontooltiptextw () functions respond to ttn_getdispinfoa messages and ttn_getdispinfow messages respectively, and provide tooltip for toolbar buttons. The two processing functions load the corresponding strings like the onmenuselect () function, but use the strings after/n. (Edge Note: The onmenuselect () and ontooltiptexta () functions are insecure for DBCS characters because it does not check the header and tail of the DBCS string when searching for/n characters) the following is an example of the toolbar and its associated help string:
7. Create Toolbar of different styles
If you do not like to display 3D buttons on the toolbar (although it is a bad thing to look at plane interface elements from the availability perspective), you can change the createsimpletoolbar () function parameters to change the toolbar style. For example, you can use the following code to create an ie-style toolbar in cmainframe: oncreate:
Createsimpletoolbar (0, atl_simple_toolbar_style | tbstyle_flat | tbstyle_list );
If you use the Wizard to add a manifest file for your program, it will use the 6.0 general control on Windows XP, you cannot select the button type, the toolbar automatically uses the flat button even if you do not add the tbstyle_flat style when creating the toolbar.
8. toolbar Editor
As we have seen before, the wizard has created several default buttons for our program, of course, only the about button has event processing. You can use the toolbar editor to modify toolbar resources like in an MFC project. createsimpletoolbarctrl () uses this toolbar to create a toolbar. The following figure shows the toolbar generated by the wizard in the Editor:
For our clock program, we add four buttons, two buttons to change the color of the View window, and the other two to show/hide the toolbar and status bar. Below is our new toolbar:
These buttons are:
- Idc_cp_colors: change the color of the View window to the color of the codeproject website.
- Idc_bw_colors: change the color of the View window to black and white
- Id_view_status_bar: displays or hides status bars.
- Id_view_toolbar: display or hide Toolbar
The first two buttons have corresponding menu items. They call a new function setcolor () of the View class to pass the foreground color and background color to this function, the View window uses these two parameters to change the display of the window. There is no difference between the processing functions that respond to the two buttons and the processing functions that respond to the corresponding menu items using the command_id_handler_ex macro. You can view the code of the example project to understand the details of message processing. In the next section, I will introduce the UI status updates of the status bar and toolbar buttons so that they can reflect the current status of the toolbar or toolbar.
9. UI status update of toolbar buttons
The code generated by the wizard has added the check and uncheck UI update processes for the View | toolbar and View | status bar menus for the cmainframe. This is the same as the program in Chapter 2: Use the UI update macro for the two commands of the cmainframe class:
Begin_update_ui_map (cmainframe) <br/> update_element (id_view_toolbar, updui_menupopup) <br/> update_element (id_view_status_bar, callback) <br/> end_update_ui_map ()
The toolbar button of our clock program has the same ID as the corresponding menu item, so the first step is to add the updui_toolbar flag to each macro:
Begin_update_ui_map (cmainframe) <br/> update_element (id_view_toolbar, items | updui_toolbar) <br/> update_element (items, items | updui_toolbar) <br/> end_update_ui_map ()
You also need to add two function response toolbar buttons to update, but fortunately the wizard has already done for us, so if you compile this program at this time, both the menu item and toolbar button will be updated.
10. Enable a toolbar to support UI status updates
If you check the code of cmainframe: oncreate (), you will find a new code that sets the initial status of two menu items:
Lresult cmainframe: oncreate (...) <br/> {<br/> //... <br/> m_hwndclient = m_view.create (...); </P> <p> uiaddtoolbar (m_hwndtoolbar); <br/> uisetcheck (id_view_toolbar, 1); <br/> uisetcheck (id_view_status_bar, 1 ); <br/> //... <br/>}
Uiaddtoolbar () transmits the window handle of the toolbar to cupdateui. Therefore, cupdateui sends a message to the window when the button status needs to be updated. Another important call is in onidle:
Bool cmainframe: onidle () <br/>{< br/> uiupdatemedilbar (); <br/> return false; <br/>}
When no message is waiting in the message queue, cmessageloop: Run () calls onidle (), and uiupdatemedilbar () traverses the UI update table to find the updui_toolbar marked by uisetcheck () such functions change the interface elements (tool bar of course) of the state and change the button State accordingly. Note that the preceding two steps are not required if the pop-up menu is updated. Because cupdateui responds to the wm_initmenupopup message, the menu status is changed only when the message is received.
If you check the sample code, you will find that it also shows how to update the status of top-level menu items in the menu bar of the Framework Window. One menu item is to execute the start and stop commands to start and stop the clock. Of course, this requires some unusual things: the menu items on the menu bar are always in the pop-up status. For a complete introduction to cupdateui, I added them to the sample code. To learn about them, I can find the call to the uiaddmenubar () and uiupdatemenubar () functions.
11. Use rebar to replace simple Toolbar
Cframeworkwimpl also supports the rebar control to make your program look like ie. Using rebar is also a method to use multiple toolbar in the program, another method is to modify the source code of wtl ). To use rebar, You must select the rebar check box on the second page of the wizard, as shown below:
The second example project wtlclock3 uses the rebar control. If you are learning the example code, open wtlclock3 now.
First, you will notice that the code for creating a toolbar is somewhat different. This is because rebar is used in the program. The following is the relevant code:
Lresult cmainframe: oncreate (...) <br/>{ <br/> hwnd hwndtoolbar = createsimpletoolbarctrl (m_hwnd, <br/> idr_mainframe, false, <br/> atl_simple_toolbar_pane_style ); </P> <p> createsimplerebar (atl_simple_rebar_noborder_style); <br/> addsimplerebarband (hwndtoolbar); <br/> //... <br/>}
The Code starts from creating a toolbar and uses different styles, that is, atl_simple_toolbar_pane_style, which is defined in atlframe. in the H file, the style is similar to that of the atl_simple_toolbar_style file, but some styles such as ccs_noparentalign are appended. This is the style necessary to make the toolbar as a child window of the rebar to work normally.
The next line of code calls the createsimplerebar () function. This function creates a rebar control and stores the handle in m_hwndtoolbar. Next, call the addsimplerebarband () function to create a bit for the rebar and tell the rebar that it is a tool bar.
The cmainframe: onviewtoolbar () function is also somewhat different. It only hides the bit of the toolbar on the rebar, rather than the m_hwndtoolbar (if m_hwndtoolbar is hidden, the entire rebar is hidden, not just the toolbar ).
If you use multiple toolbar, just create them in oncreate () and call addsimplerebarband () to add them to rebar like the code generated by the wizard about the first toolbar. Cframeworkwimpl uses standard rebar controls. Unlike MFC, cframeworkwimpl supports stopovers. You can arrange the positions of these toolbar in the rebar.
12. Status Bar of multiple panes
Wtl has another status bar class that implements Multi-pane status bars. Like the default status bar of MFC, there are caps, lock, and Num Lock indicators. This class is cmultipanestatusbarctrl, the wtlclock3 example project demonstrates how to use this class. This class supports limited UI updates. When a pop-up menu is displayed, the "default" attribute pane will extend to the width of the entire status bar for display of menu Pass help.
The first step is to declare a member variable of the cmultipanestatusbarctrl type in the cmainframe:
Class cmainframe: Public... <br/>{< br/> //... <br/> protected: <br/> cmultipanestatusbarctrl m_wndstatusbar; <br/> };
Create a status bar in oncreate () and update the UI:
M_hwndstatusbar = m_wndstatusbar.create (* This); <br/> uiaddstatusbar (m_hwndstatusbar );
As the createsimplestatusbar () function does, we also store the status bar handle in m_hwndstatusbar.
The next step is to call cmultipanestatusbarctrl: setpanes () function to create the pane:
Bool setpanes (int * ppanes, int npanes, bool bsettext = true );
Parameters:
-
Ppanes
-
Array for storing the pane ID
-
Npanes
-
Number of elements in the pane ID array)
-
Bsettext
-
If it is true, all the panes are set immediately, which is explained below.
The pane ID can be id_default_pane. This ID is used to create a pane that supports pass-by help. The pane ID can also be a string resource ID. Load the string corresponding to this ID for the non-default pane wtl, calculate the width, and set the pane to the corresponding width, which is the same as the logic used by MFC.
Bsettext controls whether the pane displays relevant strings immediately. If it is true, setpanes () displays the strings of each pane. Otherwise, the pane is left empty.
The following is our call to setpanes:
// Create the status bar panes. <br/> int anpanes [] = {id_default_pane, idpane_status, <br/> idpane_caps_indicator}; </P> <p> m_wndstatusbar.setpanes (anpanes, 3, false );
The string corresponding to idpane_status is "@", which should have enough width (hopefully) to display the two clock status strings "running" and "STOPPED ". Like MFC, You need to estimate the width of the pane by yourself. The string corresponding to idpane_caps_indicator is "Caps ".
13. UI status update in the pane
To update the text in the pane, we need to add the corresponding pane to the UI update table:
Begin_update_ui_map (cmainframe) <br/> //... <br/> update_element (1, updui_statusbar) // clock status <br/> update_element (2, updui_statusbar) // caps indicator <br/> end_update_ui_map ()
The first parameter of this macro is the index of the pane rather than the ID. Unfortunately, if you rearrange the pane, remember to update the UI update table.
Because setpanes () is called and the third parameter is false, the pane is empty at first. The next step is to set the initial text of the clock Status Pane to "running"
// Set the initial text for the clock Status Pane. <br/> uisettext (1, _ T ("running "));
As before, the first parameter is the index of the pane. Uisettext () is the only UI update function supported by the status bar.
Finally, add a call to the uiupdatestatusbar () function in cmainframe: onidle () so that the pane of the status bar can be updated during idle time:
Bool cmainframe: onidle () <br/>{< br/> uiupdatemedilbar (); <br/> uiupdatestatusbar (); <br/> return false; <br/>}
When you use uiupdatestatusbar (), a problem with cupdateui is exposed. The text of the menu item is not changed after uisettext () is called! If you are looking at the wtlclock3 project code, the start/stop menu item of the clock is moved to the clock menu, and the text of the menu item is set in the Response Processing Function of the menu item command. For whatever reason, if uiupdatestatusbar () is currently called, the call to uisettext () will not work. I have not studied whether the problem can be fixed, so if you want to change the text of the menu, you need to pay attention to it.
Finally, we need to check the status of the Caps Lock key and update the corresponding two panes. These codes are called through onidle (), so the program will check their status at each idle time.
Bool cmainframe: onidle () <br/>{< br/> // check the current Caps Lock State, and if it is on, show the // caps indicator in pane 2 of the status bar. if (getkeystate (vk_capital) & 1) uisettext (2, cstring (lpctstr (idpane_caps_indicator); else uisettext (2, _ T ("")); </P> <p> uiupdatemedilbar (); <br/> uiupdatestatusbar (); <br/> return false; <br/>}
When uisettext () is called for the first time, the "Caps" string will be loaded from the string resource, but a clever trick is used in the cstring Constructor (with adequate documentation ).
After all the code is completed, the status bar looks like this: