VS2010/MFC dialog: Creating and displaying a non-modal dialog box

Source: Internet
Author: User

Creation and display of non-modal dialog boxes

The previous section tells the modal dialog box and its pop-up process, which is followed by another dialog box-the creation and display of the Non-modal dialog box.

As has been said, the non-modal dialog box is displayed, the other windows of the program can still run normally, can respond to user input, and can switch to each other. Chicken Peck Rice will change the tip modal dialog created in the previous lecture to the Non-modal dialog box, so that you can see the effect.

dialog box Resource and dialog class for non-modal dialogs

In fact, modal dialogs and non-modal dialogs are no different in creating dialog resources and generating dialog boxes, so neither the Idd_tip_dialog dialog resource nor the Ctipdlg class created in the previous lecture need to be modified.

to create and display a non modal dialog box

What needs to be modified is that the dialog box class instance is created and displayed, that is, the dialog box that was previously added in the Cadditiondlg::onbnclickedaddbutton () function body displays the code. Here are the specific steps:

1. Include the Ctipdlg header file in AdditionDlg.h and define a pointer member variable of type Ctipdlg. To do this, remove the previously added # include "TipDlg.h" in AdditionDlg.cpp and add # include "TipDlg.h" to AdditionDlg.h. This is because we need to define a pointer variable of type CTIPDLG in AdditionDlg.h, so first include its header file, and then add the private member variable Ctipdlg *m_ for the Cadditiondlg class in AdditionDlg.h Ptipdlg;.

2. Initialize the member variable M_ptipdlg in the constructor of the Cadditiondlg class. If there are too many functions in the CPP file, we can find the Cadditiondlg class in the half view of Class View, and then find its constructor double-click in the next half view, and the intermediate client area can be immediately cut to the implementation of the constructor. Add M_ptipdlg = NULL in the constructor body; it's a good habit, chicken peck rice. In the assignment and pointer operations of pointers in the Primer series for C + + programming, it is possible to initialize any pointer variable before it is used, to avoid destroying the address's data by mistakenly accessing the important memory address.

3. Add the modal dialog box added in the previous lecture to display code comments or delete, add the non-modal dialog box to create and display code. VC + + Comments single line code using "//", note that multiple lines of code can be commented at the beginning of the code to add "/*", the end of adding "* *". The modified Cadditiondlg::onbnclickedaddbutton () function is as follows:

C + + code
  1. void Cadditiondlg::onbnclickedaddbutton ()
  2. {
  3. //Todo:add your control notification handler code here
  4. /*int_ptr nres; The return value used to hold the DoModal function
  5. Ctipdlg Tipdlg; Constructs an instance of the dialog class Ctipdlg
  6. Nres = Tipdlg.domodal (); Popup dialog box
  7. if (IDCANCEL = = nres)//Determine if the return value of the dialog box is IDCANCEL, or return if it is, continue execution down
  8. return;*/
  9. //If the value of the pointer variable m_ptipdlg is NULL, the dialog box has not yet been created and needs to be created dynamically
  10. if (NULL = = M_ptipdlg)
  11. {
  12. //Create an instance of a non-modal dialog box
  13. M_ptipdlg = new Ctipdlg ();
  14. M_ptipdlg->create (Idd_tip_dialog, this );
  15. }
  16. //Show Non modal dialog box
  17. M_ptipdlg->showwindow (Sw_show);
  18. //Save the data in each control to the appropriate variable
  19. UpdateData (TRUE);
  20. //Will be added and assigned to M_editsum by Addend and Addend
  21. M_editsum = M_editsummand + m_editaddend;
  22. update the corresponding control according to the values of each variable. And the edit box will display the value of the M_editsum
  23. UpdateData (FALSE);
  24. }

4. Because this non-modal dialog instance is created dynamically, you need to manually delete this dynamic object to destroy the dialog box. We add the delete code to the destructor of the Cadditiondlg class, but MFC does not give the destructor automatically, and we need to add it manually, and we'll call our custom destructor when the dialog object is refactored. Add a destructor declaration for CADDITIONDLG in the AdditionDlg.h file: ~cadditiondlg (), and then add the implementation of the destructor to the AdditionDlg.cpp file, as shown in the function body:

C + + code
    1. Cadditiondlg::~cadditiondlg ()
    2. {
    3. //If the Non-modal dialog box has been created, delete it
    4. if (NULL! = M_ptipdlg)
    5. {
    6. //Delete Non modal dialog object
    7. Delete M_ptipdlg;
    8. }
    9. }

In this way, the code created and displayed by the Non-modal dialog box is added and modified. Let's run a look at the effect.

Enter Summand and Addend on the Add Calculator dialog, then click the "Calculate" button, still like the previous section pop up the prompt dialog box, but do not close it, you can drag it after the addition Calculator dialog box to try, we found that the addition Calculator dialog can be dragged, and "and" The result of the operation has been shown in the edit box, which indicates that the prompt dialog has not been closed and Onbnclickedaddbutton () continues to execute downward, not only that each edit box of the addition calculator can also respond to input.

This is just a simple example, the use of Non-modal dialog box is many, later in the software development will be used.

VS2010/MFC dialog: Creating and displaying a non-modal dialog box

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.