VC MFC modeless dialog box implementation method

  • 2020-04-02 02:30:31
  • OfStack

As we all know, MFC non-modal dialog boxes can be displayed, and other Windows of the program can still run normally, can respond to user input, and can switch between each other. This article will explain to you the modeless dialog box implementation method:

Modeless dialog box dialog box resources and dialog box class

In fact, modal and modeless dialogs are no different in creating dialog resources and generating dialog classes, so neither the IDD_TIP_DIALOG resources nor the CTipDlg classes created when the modal dialog is created need to be modified.

Steps to create and display modeless dialog boxes

Need to modify the dialog class instance to create and display, namely before the CAdditionDlg: : OnBnClickedAddButton () function body add dialog displays the code. Here are the steps:

1. Include the CTipDlg header in AdditionDlg. H and define pointer member variables of type CTipDlg. To do this, remove #include "tipdlg.h" from AdditionDlg. CPP and add #include "tipdlg.h" in additiondlg.h. Then add the private member variable CTipDlg to the CAdditionDlg class in AdditionDlg. H   * m_pTipDlg; .

2. Initializes 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 Class CAdditionDlg in the upper half of the Class View, and then double click its constructor in the lower half of the Class View, and the middle client area can be immediately cut to the implementation of the constructor. Add m_pTipDlg = NULL to the constructor body. It is a good practice to initialize any pointer variables before they are used to avoid damaging the data of important memory addresses due to incorrect access.

3. Add modeless dialog box creation and display code. VC++ comments single line of code using "//", comment multiple lines of code can be added in the beginning of the code to comment "". The modified CAdditionDlg: : OnBnClickedAddButton ()

The function is as follows:


void CAdditionDlg::OnBnClickedAddButton() 
{ 
  // TODO: Add your control notification handler code here 
  /*INT_PTR nRes;       //To save the return value of the DoModal function
  CTipDlg tipDlg;      //Construct an instance of the dialog class CTipDlg
  nRes = tipDlg.DoModal(); //Pop-up dialog box
  if (IDCANCEL == nRes)   //Determine whether the return value is IDCANCEL after the dialog box exits, and if so, return, otherwise continue down
    return;*/ 
 
  //If the value of the pointer variable m_pTipDlg is NULL, the dialog has not yet been created and needs to be created dynamically
  if (NULL == m_pTipDlg) 
  { 
    //Create a modeless dialog instance
    m_pTipDlg = new CTipDlg(); 
    m_pTipDlg->Create(IDD_TIP_DIALOG, this); 
  } 
  //Displays the modeless dialog box
  m_pTipDlg->ShowWindow(SW_SHOW); 
 
  //Saves the data in each control to the corresponding variable
  UpdateData(TRUE); 
 
  //Assign the sum of the plus and the plus to m_editSum
  m_editSum = m_editSummand + m_editAddend; 
 
  //Update the corresponding control according to the value of each variable. The edit box of and displays the value of m_editSum
  UpdateData(FALSE); 
}

4. Because this modeless dialog instance is created dynamically, you need to manually delete this dynamic object to destroy the dialog. We add delete code in the destructor of the class CAdditionDlg, but the destructor is not given automatically by MFC. In this case, we need to manually add the destructor, which will call our custom destructor when the dialog object is destructed. Add a destructor declaration for CAdditionDlg in AdditionDlg. H: ~CAdditionDlg(); Then add an implementation of the destructor in additionpodg.cpp. The body of the function is as follows:


  CAdditionDlg::~CAdditionDlg() 
  { 
    //Delete the modeless dialog if it has been created
    if (NULL != m_pTipDlg) 
    { 
      //Deletes the modeless dialog object
      delete m_pTipDlg; 
    } 
  } 

In this way, the modeless dialog is created and displayed with added modifications.
This is just a simple example, so I believe you can use the dialog box to a higher level, you can choose to use the modal dialog box and modeless dialog box in different situations.


Related articles: