(reprinted) Introduction to VS2010/MFC programming 23 (Common controls: Programming instances of Button controls)

Source: Internet
Author: User

Previous section VS2010/MFC programming Getting Started Tutorial Chicken Peck Rice Speaking the basic usage of Button control button, Radio button and check box, this section continues to talk about the contents of the button control, through an instance to make it clearer how the button control is used in the actual software development.

Because the button control in the previous example involves, relatively simple, this article does not make in-depth analysis, but focus on the radio button radio button, check box use.

function of the button control instance

The functionality that this instance implements is introduced first. This instance is used to select a site based on the type of site and to display the name of the selected site in the edit box. There are three types of Web sites, such as portals, forums, and blogs, which are radio buttons. The site has six: Chicken peck rice, Sina, Tianya Forum, Han Cold blog, NetEase and Phoenix Forum, are check box.

When a radio button is clicked on a site type that is selected, the check box for its corresponding site is activated, others are disabled, no selection is allowed, and it is unchecked. For example, if the Portal radio button is selected, the Sina, NetEase check boxes are activated, the user is allowed to select, and the other checkboxes are disabled.

Implementation of a Button control instance

Chicken Peck Rice below for everyone to elaborate on this example of the writing steps.

1. Create a dialog-based MFC project with the name "Example23".

2. In the Idd_example23_dialog template of the auto-generated main dialog box, delete the Todo:place dialog controls here. Static text box, add two group Box, attribute caption to "site Type", "Web Site", respectively.

3. Add three RADIO button,caption to "portal", "Forum" and "blog" in Group box "site Type", respectively, with IDs Idc_portal_radio, Idc_forum_radio and Idc_blog_ RADIO.

4. In the Group box "website" To add six check box,caption respectively set as "chicken peck rice", "Sina", "Tianya Forum", "Han Han blog", "NetEase" and "Phoenix Forum", id respectively set to Idc_check1, Idc_check2, idc_ CHECK3, Idc_check4, Idc_check5 and Idc_check6. Then add variables of type CButton, M_check1, M_check2, M_check3, M_check4, M_check5, and m_check6 for each check box.

5. Under Two group box, add a static text box and an edit box. The caption of the static text box is set to "Selected sites:". The ID of the edit box is set to Idc_website_sel_edit, and the property read only changes to true so that the edit box is read-only and is not allowed to be edited by the user.

6. Modify the caption of the "OK" button to "OK", and the "Cancel" button to "caption" to "Exit". In this case, the dialog box template is modified, such as:

7. Add a message handler for the "portal", "forum", and "blog" three radio buttons respectively Cexample23dlg::onbnclickedportalradio (), Cexample23dlg::o Nbnclickedforumradio () and Cexample23dlg::onbnclickedblogradio ().

After a radio button is clicked, we can first disable the six web site check box and leave it unchecked, and then activate the site check box for the selected site type. For code reuse, we'll write all the checkboxes to the disabled and unchecked state to a function, which is cexample23dlg::initallcheckboxstatus (), You can then call Initallcheckboxstatus () in the message handler for three radio buttons to initialize the state of the check box.

The implementation of the three message processing functions and the Initallcheckboxstatus () function is as follows:

C + + code
  1. void Cexample23dlg::onbnclickedportalradio ()
  2. {
  3. //Todo:add your control notification handler code here
  4. //If the "Portal" radio button is selected, activate the checkbox "Sina" and "NetEase", the other check boxes are disabled not selected
  5. Initallcheckboxstatus ();
  6. M_check2. EnableWindow (TRUE);
  7. M_check5. EnableWindow (TRUE);
  8. }
  9. void Cexample23dlg::onbnclickedforumradio ()
  10. {
  11. //Todo:add your control notification handler code here
  12. //If the "Forum" radio button is selected, activate the checkbox "Tianya Forum" and "Phoenix Forum", other check boxes are disabled not selected
  13. Initallcheckboxstatus ();
  14. M_check3. EnableWindow (TRUE);
  15. M_check6. EnableWindow (TRUE);
  16. }
  17. void Cexample23dlg::onbnclickedblogradio ()
  18. {
  19. //Todo:add your control notification handler code here
  20. //If the "blog" radio button is selected, the checkbox "Chicken Peck Rice" and "Han Han blog" are activated, other check boxes are disabled not selected
  21. Initallcheckboxstatus ();
  22. M_check1. EnableWindow (TRUE);
  23. M_check4. EnableWindow (TRUE);
  24. }
  25. Initializes the state of all check boxes, which are all disabled, all unchecked
  26. void Cexample23dlg::initallcheckboxstatus ()
  27. {
  28. //Disable all
  29. M_check1. EnableWindow (FALSE);
  30. M_check2. EnableWindow (FALSE);
  31. M_check3. EnableWindow (FALSE);
  32. M_check4. EnableWindow (FALSE);
  33. M_check5. EnableWindow (FALSE);
  34. M_check6. EnableWindow (FALSE);
  35. //All unchecked
  36. M_check1. SetCheck (0);
  37. M_check2. SetCheck (0);
  38. M_check3. SetCheck (0);
  39. M_check4. SetCheck (0);
  40. M_check5. SetCheck (0);
  41. M_check6. SetCheck (0);
  42. }

8. After the program runs, we want the site type to be selected by default as "portal", then modify the dialog initialization function Cexample23dlg::oninitdialog () to:

C + + code
  1. BOOL Cexample23dlg::oninitdialog ()
  2. {
  3. Cdialogex::oninitdialog ();
  4. //Add "About ..." menu item to System menu.    
  5. //Idm_aboutbox must is in the System command range.    
  6. ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox);
  7. ASSERT (Idm_aboutbox < 0xf000);
  8. cmenu* Psysmenu = GetSystemMenu (FALSE);
  9. if (psysmenu! = NULL)
  10. {
  11. BOOL Bnamevalid;
  12. CString Straboutmenu;
  13. Bnamevalid = straboutmenu.loadstring (Ids_aboutbox);
  14. ASSERT (Bnamevalid);
  15. if (!straboutmenu.isempty ())
  16. {
  17. Psysmenu->appendmenu (Mf_separator);
  18. Psysmenu->appendmenu (mf_string, Idm_aboutbox, Straboutmenu);
  19. }
  20. }
  21. //Set The icon for this dialog. The framework does this automatically
  22. //When the application ' s main window is not a dialog
  23. SetIcon (M_hicon, TRUE); //Set big icon
  24. SetIcon (M_hicon, FALSE); //Set small icon
  25. //Todo:add extra initialization here
  26. //The Portal radio button is selected by default
  27. CheckDlgButton (Idc_portal_radio, 1);
  28. Onbnclickedportalradio ();
  29. return TRUE; //Return TRUE unless you set the focus to a control
  30. }

9. Click "OK" to display the selected site name to the edit box, then you need to modify the "OK" button (the original OK button) message processing function Cexample23dlg::onbnclickedok () is as follows:

C + + code
  1. void Cexample23dlg::onbnclickedok ()
  2. {
  3. //Todo:add your control notification handler code here
  4. CString Strwebsitesel; //Selected websites
  5. //If "Chicken Peck Rice" is selected, add it to the result string
  6. if (1 = = M_check1. Getcheck ())
  7. {
  8. Strwebsitesel + = _t ("Chicken peck rice");
  9. }
  10. //If "Sina" is selected, add it to the result string
  11. if (1 = = M_check2. Getcheck ())
  12. {
  13. Strwebsitesel + = _t ("Sina");
  14. }
  15. //If "Tianya forum" is selected, add it to the result string
  16. if (1 = = M_check3. Getcheck ())
  17. {
  18. Strwebsitesel + = _t ("Tianya Forum");
  19. }
  20. //If "Han Han blog" is selected, add it to the result string
  21. if (1 = = M_check4. Getcheck ())
  22. {
  23. Strwebsitesel + = _t ("Korean Cold blog");
  24. }
  25. //If "NetEase" is selected, add it to the result string
  26. if (1 = = M_check5. Getcheck ())
  27. {
  28. Strwebsitesel + = _t ("NetEase");
  29. }
  30. //If "Phoenix Forum" is selected, add it to the result string
  31. if (1 = = M_check6. Getcheck ())
  32. {
  33. Strwebsitesel + = _t ("Phoenix Forum");
  34. }
  35. //Display the result string in the edit box after "Selected Web site"
  36. Setdlgitemtext (Idc_website_sel_edit, Strwebsitesel);
  37. //In order to avoid the point "OK" after the dialog box exits , the OnOK is dropped
  38. //cdialogex::onok ();    
  39. }

10. Completion of the program writing. To run the program Pop-up dialog box, select the post-site interface such as:

The contents of the button control are that. Master the basic use of the button control, and then hand-written this instance, I believe you are familiar with the button control. Chicken Peck Rice welcome you to continue to learn communication.

Original address: http://www.jizhuomi.com/software/184.html

(reprinted) Introduction to VS2010/MFC programming 23 (Common controls: Programming instances of Button 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.