The previous tutorial dealt with the
Creation of Modal Dialog boxes in MFC. This part of our Tutorial deals with the
Creation of Modeless Dialog boxes in MFC.
The difference between a modal and modeless dialog box is
that, modal dialogs once invoked will not allow the users to access
the parent window, where as modeless dialogs will allow the user
to work with the parent window. A user cannot enter inputs in
any other dialog or invoke a menu option except without
explicitly closing the modal dialog, within the application. But
the user can leave the modeless dialog open and do anything
after the modless dialog is invoked.
Modeless Dialog Box handling also is done using CDialog class in MFC. CDialog
is a CWnd derived class with some extra facilities for dialog
handling. The initialization of the dialog is done using an
over-ridable function OnInitDialog. The dialogs are
closed by calling the function EndDialog. The dialogs can
accommodate a lot of controls like Edit controls, static
controls, list boxes, combo boxes, progress bars, list views,
tree views and many more.
All the
above mentioned controls have their own classes for handling
themselves. All of them including dialog box, also have their
own message handling mechanisms involving message maps. They all
are treated as window/s, except with some special
characteristics.
Let us see a step by
step procedure for adding a modeless dialog box to our application in
this MFC Tutorial. If you look at the above explanation above,
it is simply a copy paste of the Modal dialog handling. As such
the above code of handling the controls inside a dialog is
exactly the same as in Modal dialogs. It is only the way of
calling/invoking the dialog which is different. Similar to the
previous
Modal dialog MFC Tutorial, this tutorial also needs the knowledge of Creating
a window MFC
Tutorial part I and Menu creation as in MFC
Tutorial Part IV.
The sample code and description
below demonstrates the creation of Modeless
dialogs in MFC.
Step1:
Create a new
project of type Win32 application. In the second screen of the
wizard, select the first option "Create an empty project". Do
not allow the wizard to add any files.
Step 2:
After the project is created, click on Menu -->Project --> Add
to Project -->New and select a .cpp file and give a name to it.
Step 3:
Copy and paste the code below.
//MFC6.CPP - Modeless Dialogs in MFC from CoderSource.net
#include <afxwin.h>
class MFC_Tutorial_Window :public
CFrameWnd
{
public:
MFC_Tutorial_Window()
{
Create(NULL,"MFC Tutorial Part 6 - Modeless Dialog Box");
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
END_MESSAGE_MAP()
class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
public:
BOOL
InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(1);
return 1;
}
};
MyApp theApp;
//This code snippet has the capacity to create a window
Now we have to add the dialog resource and then add the
necessary code for it
Step 4:
Click on the Menu --> Insert --> Resource --> "Dialog" and click
New command button. A default dialog will be created
with two command buttons.
Step 5:
On the left hand side, on the Resources view if the Dialog tree is expanded, the
resource id of the newly created dialog will be displayed. Just
press Enter key on the dialog box and change the dialog
id to IDR_MYDIALOG.
Step 6:
Save
the resource. It will give a file name as script1.rc. Be careful
to save it to the path of your project.
Step
7:
Now click on Menu --> View --> Class
Wizard or press Ctrl + W. You will be presented with a dialog
asking you, if you want to create a new class. Say Yes. The
wizard will create a new dialog class and related files(
Newdialog.h & newdialog.cpp) for you.
Step 8:
Open the header file of your new dialog "NewDialog.h" and add
the include directive for <afxwin.h>. Also add the "NewDialog.h"
include directive to your project's MFC5.cpp file that you have
created in the beginning.
Step 9:
Create a menu
resource and add the following code as highlighted in bold
letters as follows.
//MFC5.CPP - MFC Tutorial Part 5 from
CoderSource.net
#include <afxwin.h>
#include "resource.h"
#include "newdialog.h"
class MFC_Tutorial_Window
:public CFrameWnd
{
public:
MFC_Tutorial_Window()
{
Create(NULL,"MFC Tutorial Part 6 - Modeless Dialog Box");
}
void OnClickDialogNew();
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(
MFC_Tutorial_Window, CFrameWnd)
ON_COMMAND(ID_MYDIALOG,OnClickDialogNew)
END_MESSAGE_MAP()
void
MFC_Tutorial_Window::OnClickDialogNew()
{
NewDialog *dlg;
dlg = new NewDialog;
dlg->Create(IDR_MYDIALOG);
dlg->ShowWindow(1);
}
class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
public:
BOOL
InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(1);
return 1;
}
};
MyApp theApp;
Now, if the menu item is clicked, the dialog will be shown with
the two default buttons. If either the Enter key or the escape
key is pressed, the dialog will be closed. Please find the
sample code
for this article here. Read here for knowing how
to avoid closing the dialog when ENTER or ESCAPE key is
pressed.