VC ++ technical insider (Fourth Edition) Note (chapter 2)

Source: Internet
Author: User

Chapter 2: toolbar and status bar

1. the toolbar is an object of the ctoolbar class, And the status bar is the object of the cstatusbar class. The ctoolbar class and the cstatusbar class are derived from the ccontrolbar class. The ccontrolbar class is derived from the cwnd class.
The control bar windows supported by the ccontrolbar class are located in the main frame window, and these control bar windows can automatically adjust their size and position as the size of the main frame window changes or moves.
The construction, analysis, and Window Creation of the control bar objects are managed by the application framework. The code generated by the Wizard is located in the framework header file and code file.

2. When a button in the toolbar is pressed, the corresponding command message is sent like a menu or a shortcut key (the command message is transmitted like a menu command. cause: toolbar objects and status bar objects are all associated with the main frame window .).
The update command UI message control function can be used to update the button status in the toolbar and modify the button graphics in the toolbar.
There is only one bitmap in the toolbar. Each button occupies a 15-pixel, 16-pixel-wide bitmap.
Note: Do not directly edit the toolbar bitmap. Instead, use the toolbar editing tool specified by idea studio to edit the toolbar.

3. The message control function of the update command is mainly used to disable and check menu items, but it can also act on toolbar buttons.
1) ccmdui: Enable
Virtual void enable (bool bon = true );
// Bon: True to enable the item, false to disable it.
// Call this member function to enable or disable the user-interface item for this command.
2) ccmdui: setcheck
Virtual void setcheck (INT ncheck = 1 );
// Ncheck: Specifies the check State to set. If 0, unchecks; if 1, checks; and if 2, sets indeterminate.
// Call this member function to set the user-interface item for this command to the appropriate check state. this member function works for menu items and toolbar buttons. the indeterminate state applies only to toolbar buttons.

4. When the pop-up menu of a menu item pops up, the UI message control function of the update command for processing the menu item will be called.
The toolbar is always displayed. The UI control function of the update command for processing it is called during the process of processing the null state of the application.
When the UI control function of the same update command needs to process a menu item and a toolbar button, the control function is called during the null state processing process and when the pop-up menu of the menu item is displayed.

5. Tips for creating a tool:
Add the prompt text behind the menu prompt and add a line break before it. For example, in prompt: print the activity document/n print

6. Several Important Functions
1) cwnd: getparentframe
Cframewnd * getparentframe () const;
// Return value: a pointer to a frame window if successful; otherwise null.
// Call this member function to retrieve the parent frame window. The member function searches up the parent chain until a cframewnd (or derived class) object is found.
// The MDI child frame window is obtained in MDI.
2) afxgetapp
Cwinapp * afxgetapp ();
// Return value: a pointer to the single cwinapp object for the application.
// The pointer returned by this function can be used to access application information such as the main message-Dispatch code or the topmost window.
Get the main framework window pointer through the Application: (applicable to single-document and multi-document programs)
Cmainframe * pframe = (cmainframe *) afxgetapp ()-> m_pmainwnd;
Ctoolbar * ptoolbar = & pframe-> m_wndtoolbar;
Note: In the SDI Program, when the oncreate function of the view is called, m_pmainwnd is not set. In this case, you can use cwnd: getparentframe to obtain the window pointer of the main frame.
Note:
In the MDI program, the Appwizard wizard automatically generates a value assignment code for m_pmainwain. in SDI, the Framework assigns values to m_pmainwain during the creation of the view.

7. Use the toolbar Editor: select a button with the mouse, and press the Del key to erase the pixels of the button. to delete a button, you only need to drag it out of the toolbar.

8. tool bar DOCK:
You can see the following code in the cmainframe: oncreate function, which allows the toolbar to dock at any side of the frame window:
M_wndtoolbar.enabledocking (cbrs_align_any );
Enabledocking (cbrs_align_any );
Dockcontrolbar (& m_wndtoolbar );
Note:
1) ccontrolbar: enabledocking
// Call this function to enable a control bar to be docked. the sides specified must match one of the sides enabled for docking in the destination frame window, or the control bar cannot be docked to that frame window.
2) cframewnd: enabledocking
// Call this function to enable dockable control bars in a frame window. by default, control bars will be docked to a side of the frame window in the following order: top, bottom, left, right.
3) cframewnd: dockcontrolbar
// Causes a control bar to be docked to the frame window. the control bar will be docked to one of the sides of the frame window specified in the callto both ccontrolbar: enabledocking and cframewnd: enabledocking. the side chosen is determined by ndockbarid.

9. the status bar neither accepts user input nor generates command messages. It displays some text in the pane under the control of the program.
The status bar supports two types of text pane (Information row pane and Status Indicator pane ).
Note: To display some special application data in the status bar, you must first disable the standard status bar from displaying the menu prompt and keyboard status. (How can this problem be solved ?)
1) The static array indicators generated by Appwizard in the mainfrm. cpp file is used to define the status bar.
Static uint indicators [] =
{
Id_separator, // status line indicator
Id_indicator_caps, // The following are string resource IDS
Id_indicator_num,
Id_indicator_scrl,
};
2) cstatusbar: setindicators // set the status bar based on the indicators [] array content.
Bool setindicators (const uint * lpidarray, int nidcount );
// Sets each indicator's ID to the value specified by the corresponding element (corresponding element) of the array lpidarray, loads the string resource specified by each ID, and sets the indicator's text to the string.
// Setindicators is called in the derived framework class of the application. It can be seen in the cmainframe: oncreate function.
3) The information row pane displays the strings provided by saving dynamically.
Setting information rows: first access the status bar object, and then use the index parameter starting from 0 to call the cstatusbar: setpanetext function settings.
For example:
Cmainframe * pframe = (cmainframe *) afxgetapp ()-> m_pmainwnd;
Cstatusbar * pstatus = & pframe-> m_wndstatusbar;
Pstatus-> setpanetext (0, "message line for first pane ");
4) The Status Indicator pane is always connected to a string, which is provided by string resources (set in string table ), whether it is displayed depends entirely on the UI control function of the corresponding update command.
The indicator is identified by a string resource ID, which is also used to pass the update command UI message.
Afx_msg void cmainframe: onupdatekeycapslock (ccmdui * pcmdui );
On_update_command_ui (id_indicator_caps, onupdatekeycapslock)
Void cmainframe: onupdatekeycapslock (ccmdui * pcmdui)
{
Pcmdui-> enable (: getkeystate (vk_capital) & 1 );
}
Note:
The UI message control function of the update command of the status bar is called in the idle processing phase (presumably, the ccmdui pointer is passed in when the call is made ).
5) The length of the Status Indicator pane is the length of the corresponding string resource.

10. Obtain the status bar control.
1) the update command UI control function for the keyboard status indicator is embedded in the main frame window class and associated with the resource string ID (the string resource ID defined in indicators.
2) the status bar actually has a subwindow (namely, status line indicator). Its default ID is set to afx_idw_status_bar in the cmainfrm: oncreate function cstatusbar: Create Function. by changing this ID, you can disable the frame from displaying the menu prompt in the pane 0.
The specific implementation is as follows:
Change the cstatusbar: create function in the cmainfrm: oncreate function from m_wndstatusbar.create (this) to m_wndstatusbar.create (this, ws_child | ws_visible | cbrs_bottom, callback (custom ID )).

11. Note that the left and right buttons have the same virtual key code as the keys on the keyboard. You can use the getkeystate function to obtain them.

We strongly recommend that you implement two examples in the book by yourself, especially the second one. Once you have done so, you will understand what this chapter is about.

///////////////////
//////////////////

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.