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
- void Cexample23dlg::onbnclickedportalradio ()
- {
- //Todo:add your control notification handler code here
- //If the "Portal" radio button is selected, activate the checkbox "Sina" and "NetEase", the other check boxes are disabled not selected
- Initallcheckboxstatus ();
- M_check2. EnableWindow (TRUE);
- M_check5. EnableWindow (TRUE);
- }
- void Cexample23dlg::onbnclickedforumradio ()
- {
- //Todo:add your control notification handler code here
- //If the "Forum" radio button is selected, activate the checkbox "Tianya Forum" and "Phoenix Forum", other check boxes are disabled not selected
- Initallcheckboxstatus ();
- M_check3. EnableWindow (TRUE);
- M_check6. EnableWindow (TRUE);
- }
- void Cexample23dlg::onbnclickedblogradio ()
- {
- //Todo:add your control notification handler code here
- //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
- Initallcheckboxstatus ();
- M_check1. EnableWindow (TRUE);
- M_check4. EnableWindow (TRUE);
- }
- Initializes the state of all check boxes, which are all disabled, all unchecked
- void Cexample23dlg::initallcheckboxstatus ()
- {
- //Disable all
- M_check1. EnableWindow (FALSE);
- M_check2. EnableWindow (FALSE);
- M_check3. EnableWindow (FALSE);
- M_check4. EnableWindow (FALSE);
- M_check5. EnableWindow (FALSE);
- M_check6. EnableWindow (FALSE);
- //All unchecked
- M_check1. SetCheck (0);
- M_check2. SetCheck (0);
- M_check3. SetCheck (0);
- M_check4. SetCheck (0);
- M_check5. SetCheck (0);
- M_check6. SetCheck (0);
- }
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
- BOOL Cexample23dlg::oninitdialog ()
- {
- Cdialogex::oninitdialog ();
- //Add "About ..." menu item to System menu.
- //Idm_aboutbox must is in the System command range.
- ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox);
- ASSERT (Idm_aboutbox < 0xf000);
- cmenu* Psysmenu = GetSystemMenu (FALSE);
- if (psysmenu! = NULL)
- {
- BOOL Bnamevalid;
- CString Straboutmenu;
- Bnamevalid = straboutmenu.loadstring (Ids_aboutbox);
- ASSERT (Bnamevalid);
- if (!straboutmenu.isempty ())
- {
- Psysmenu->appendmenu (Mf_separator);
- Psysmenu->appendmenu (mf_string, Idm_aboutbox, Straboutmenu);
- }
- }
- //Set The icon for this dialog. The framework does this automatically
- //When the application ' s main window is not a dialog
- SetIcon (M_hicon, TRUE); //Set big icon
- SetIcon (M_hicon, FALSE); //Set small icon
- //Todo:add extra initialization here
- //The Portal radio button is selected by default
- CheckDlgButton (Idc_portal_radio, 1);
- Onbnclickedportalradio ();
- return TRUE; //Return TRUE unless you set the focus to a control
- }
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
- void Cexample23dlg::onbnclickedok ()
- {
- //Todo:add your control notification handler code here
- CString Strwebsitesel; //Selected websites
- //If "Chicken Peck Rice" is selected, add it to the result string
- if (1 = = M_check1. Getcheck ())
- {
- Strwebsitesel + = _t ("Chicken peck rice");
- }
- //If "Sina" is selected, add it to the result string
- if (1 = = M_check2. Getcheck ())
- {
- Strwebsitesel + = _t ("Sina");
- }
- //If "Tianya forum" is selected, add it to the result string
- if (1 = = M_check3. Getcheck ())
- {
- Strwebsitesel + = _t ("Tianya Forum");
- }
- //If "Han Han blog" is selected, add it to the result string
- if (1 = = M_check4. Getcheck ())
- {
- Strwebsitesel + = _t ("Korean Cold blog");
- }
- //If "NetEase" is selected, add it to the result string
- if (1 = = M_check5. Getcheck ())
- {
- Strwebsitesel + = _t ("NetEase");
- }
- //If "Phoenix Forum" is selected, add it to the result string
- if (1 = = M_check6. Getcheck ())
- {
- Strwebsitesel + = _t ("Phoenix Forum");
- }
- //Display the result string in the edit box after "Selected Web site"
- Setdlgitemtext (Idc_website_sel_edit, Strwebsitesel);
- //In order to avoid the point "OK" after the dialog box exits , the OnOK is dropped
- //cdialogex::onok ();
- }
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)