Sunday, May 26, 2013

modal and modalless dialog in MFC

For both cases, you should create one dialog template, which could be done in the resource explorer

Modal Dialog
1. then create one class ModalDlg for modal dialog using this dialog template, and the CModalDlg.cpp and CModalDlg.h would be generated.

2. in the file where you want to show the modal dialog, first include the "ModalDlg.h", then in this file, you could use the following code :

void CChildView::OnModal() // show the modal dialog
{
 CModalDlg dlg;
 dlg.DoModal();
}


ModalLess Dialog

1. then create one class ModalLessDlg for modalless dialog using this dialog template, and the CModalLessDlg.cpp and CModalLessDlg.h would be generated.

2. in the file where you want to show the modalless dialog, first include the "ModalLessDlg.h", then in this file, you could use the following code :

void CChildView::OnModalLess() // show the modalless dialog
{
 CModalLessDlg *lsdlg=new CModalLessDlg;
 lsdlg->Create(IDD_DIALOG2,NULL);//IDD_DIALOG2 is the ID of dialog template you created
 lsdlg->ShowWindow(SW_SHOW);

 }


For both dialogs, if you want to initialize some parameters for all the controls on them, you can do that by overriding the  BOOL CModalDlg::OnInitDialog(){}  or  BOOL CModalLessDlg::OnInitDialog() {}





No comments:

Post a Comment