Introduction to VS2010/MFC Programming 54 (Ribbon Interface Development: using more controls and adding message handlers for controls)

Source: Internet
Author: User

Http://www.jizhuomi.com/software/255.html

The chicken peck rice in the previous section describes the method for adding controls to the ribbon bar. This section of the Tutorial Chicken Peck rice will continue to refine the previous examples, explain how some of the more complex controls are added, and how to add message handler functions to them.

One, add more Ribbon controls to the ribbon bar

Chicken Peck Rice will continue to add a drop-down menu, Check box, Combo box, and other ribbon controls, based on the previous example.

1. First, the "click" button on the "Small button" Panel is transformed into a drop-down menu. The "click" button has a Behavior Property menu Items, default to empty, a browse button appears to the right of it, and clicking the browse button pops up the "Items Editor" dialog box, such as:

We can select the button in the combo box under items, split line, etc., click the Add button to the right of the combo box to add it to the drop-down menu, add the button and in the Properties group you can set the caption (title), ID, image (picture) and other attributes. Chicken Peck Rice Here Add two buttons, caption properties for one Click, Double Click,id respectively for Id_one_click, Id_double_click,image and other properties are not set. The ribbon bar at this time is as follows:

A downward arrow appears to the right of the "click", and clicking on this arrow will display a drop-down menu with one click and a double click button. In addition, there is a button chicken peck rice with a red line indicating its cue message-"Test Ribbon", click on this button we can not run the program and directly see the Ribbon interface effect.

2. Add a panel "more Controls" to the right of the small button panel, then locate the check box and combo box control in the Toolbox tool and drag it into the new panel, the check box's Caption property is set to "Websites Enable ", Combo Box's properties are set to" Websites ". As follows:

We add two drop-down options to the websites combo box (Combo box) by right-clicking the websites combo box, selecting "Properties", displaying its property page, and modifying the Data property to "www.jizhuomi.com; Www.jizhuomi.com/android ", so you can add two URL options to this combo box.

Ii. Adding a message handler function for the Ribbon control

The preceding controls are all added, so we'll add a message handler for the control.

1, first for the OPEN button to add a Click event Message handler, whose ID is modified to Id_open_button, then right click on the Open button, select "Add Event Handler", pop Up the Event Handler Wizard dialog box, Select "CMainFrame" in the class list on the right, select "COMMAND" in the message type on the left, and then click the "Add and Edit" button to add void Cmainframe::o in the CMainFrame class. The Nopenbutton () member function.

As you may feel, the process of adding a message handler is similar to the previous normal control. The last function to modify the void Cmainframe::onopenbutton () function is implemented as follows:

C + + code
    1. void Cmainframe::onopenbutton ()
    2. {
    3. //Todo:add your command handler code here
    4. MessageBox (_t ("Open button!")); //Pop-up dialog box, prompting "Open button!"
    5. }

Because it is just to explain the addition of the message handler function of the button, there is no complex code written, only one statement that pops up the MessageBox. To run the program, click the Open button in the Big button panel of the function category in the result interface to bring up a dialog box and display "Open button!".

2, we add the message handler function for the Websites combo box (the same way as above), and also add the command message handler function--void Cmainframe::onwebsitescombo () to the CMainFrame class. Modifying this function is implemented as follows:

C + + code
  1. void Cmainframe::onwebsitescombo ()
  2. {
  3. //Todo:add your command handler code here
  4. //Get a pointer to the combo box control
  5. cmfcribboncombobox* Pcombobox = Dynamic_downcast (CMFCRibbonComboBox, M_wndribbonbar.findbyid (ID_WEBSITES_COMBO));
  6. //Gets the index of the currently selected item of the combo box control
  7. int ncursel = Pcombobox->getcursel ();
  8. if (ncursel >= 0)
  9. {
  10. //If the index is greater than or equal to 0, the selected item appears, and the dialog box pops up and displays information about the selected item
  11. MessageBox (Pcombobox->getitem (Ncursel));
  12. }
  13. Else
  14. {
  15. //If the index is less than 0, the item is not checked and a popup dialog prompts the user to make a selection
  16. MessageBox (_t ("Please select one item!"));
  17. }
  18. }

Run the program, in the results screen, change the websites combo box's selected item, the popup dialog box will display the information of the selected item. Such as:

3. Then we add the message handler function for the Websites enable check box. By default, the check box control does not change state because of the user's click, which requires us to implement the normal check function through the code.

We need a variable to hold the check box's current selection, so add a BOOL member variable m_bwebsitesenable to the CMainFrame class in the MainFrm.h file and initialize it in the constructor of the CMainFrame class:

C + + code
    1. Cmainframe::cmainframe ()
    2. {
    3. //TODO:ADD member initialization code here
    4. Theapp.m_napplook = Theapp.getint (_t ("Applicationlook"), Id_view_applook_off_2007_blue);
    5. //Initialize to True, that is, check box is selected
    6. m_bwebsitesenable = TRUE;
    7. }

Next, you still add the command message handler function in the CMainFrame class to the Websites enable check box in the 1 method, and modify its function body as follows:

C + + code
    1. void Cmainframe::onwebsitescheck ()
    2. {
    3. //Todo:add your command handler code here
    4. //For m_bwebsitesenable, that is, the status of the Toggle check box
    5. m_bwebsitesenable =!m_bwebsitesenable;
    6. }

But now the status of the check box is only saved to the variable, we also have to change the check box display state according to the variable value, this need to add a UPDATE_COMMAND_UI message handler function for check box, the method is still right click check box, select "Add Event Handler ", select" CMainFrame "in the class list of the Event Handler Wizard dialog box that pops up, and the Message type selects update_command_ui, the last point" Add and Edit, this adds the UPDATE_COMMAND_UI message handler function, which modifies the function as follows:

C + + code
    1. void Cmainframe::onupdatewebsitescheck (CCmdUI *pcmdui)
    2. {
    3. //Todo:add Your command update UI handler code here
    4. //Set check box state based on current variable value
    5. Pcmdui->setcheck (m_bwebsitesenable);
    6. }

At this point you can try to run the next program, click the website enable checkbox, it has been able to successfully change the status.

4, in addition to the above features, we also want to achieve a slightly more complex function, that is, if the Websites enable check box is selected to activate the websites combo box, and if unchecked, disable the websites combo box. This requires us to add a UPDATE_COMMAND_UI message handler for the websites combo box, adding the same method as above, chicken peck rice here no longer repeat. Modify the function implementation to:

C + + code
    1. void Cmainframe::onupdatewebsitescombo (CCmdUI *pcmdui)
    2. {
    3. //Todo:add Your command update UI handler code here
    4. the status of the Enable check box according to websites is really active or disabled
    5. Pcmdui->enable (m_bwebsitesenable);
    6. }

Run the program, try to change the state of the websites enable checkbox, and websites the enabled state of the combo box will also change.

Finally, the chicken peck rice then simply tells how to add new items for the Round menu button in the upper left corner of the ribbon bar and the Quick Access Toolbar.

The properties page of the Round menu button has a buttons property that can be clicked on its right-hand navigation button to pop up the Items Editor dialog box, which allows you to add a button in the lower-right corner of the menu button's pop-up menu window. There is also a main items property, where the right-hand navigation button also pops up the Items Editor dialog box, which allows you to add menu items to the Round menu button.

The Quick Access Toolbar's property page has a Qat items property, and clicking its right-side browse button pops up the Qat Items Editor dialog box, which allows you to add new items to the Quick Access Toolbar.

This section is a bit more, but it's not complicated, many of which are similar to previous knowledge. Finally Chicken peck Rice welcome everyone back to the site to see.

Introduction to VS2010/MFC Programming 54 (Ribbon Interface Development: using more controls and adding message handlers for controls)

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.