Modeless Dialogs in MFC
Let us see this in details of implementation
How Modal and Modeless Dialogs are implemented by Windows
How Modal Dialog works
How Modeless Dialog works
There are two types of dialog boxes they are modal and modeless. The most common dialog boxes are modal. A modal dialog box demands a response before the parent program will continue which means a modal dialog box will not allow you to refocus input to another part of the parent application without first responding to the dialog box. A modeless dialog box does not prevent the parent program from running which means it does not demand a response before input can be re-focused to another part of the program.
To create a modeless dialog box you must follow a two step process:-
1. First you must create an empty CDialog object which is not associated with a dialog box resource template.
2. You must associate that object witha dialog box resouce using the CDialog member function create().
To create a modeless dialog box object,use the following version of the CDialog constructor:
As you can see this constructor does not take any parameters its purpose is to instantiate the necessary mechanism to manage a modeless dialog box.This constructor is declared as protected inside CDialog which means it can be executed only by members of a class derived from CDialog one cannot use this form outside the derived class.
Once you have instantiated a dialog box object you can link it to a dialog box template using Create(),which is shown here:
BOOL CDialog::Create(LPCSTR lpszDialogName,CWnd *OWner = NULL);
BOOL CDialog::Create(UINT ID,CWnd *Owner = NULL);
In the first form lpszDialogName specifies the name of the dialog box template in the resource file. In the second form,ID is the ID of the dialog box template. In both cases,Owner is a pointer to the parent window object that owns the dialog box.If Owner is NULL,then the main application window is the parent. Create() returns non zero if sucessful and zero otherwise. After calling Create(),the modeless dialog box will be active.
Modeless dialog box object that you create may stay in existence till the dialog box is used so one has to declare it as global object or static local object. If the object goes out of scopethen the dialog box will be destroyed by the destructor of CDialog's destructor.
Modeless dialog box is not automatically visible you include the WS_VISIBLE style dialog box template definition. One may call ShowWindow() to cause it to be displayed after it has been created.Using WS_VISIBLE is easier because the box is displayed automatically.To close a modeless dialog box,your program must call DestroyWindow().
It is a member of CWnd and its prototype is shown here:
As already stated A modal dialog box demands a response before the parent program will continue which means a modal dialog box will not allow you to refocus input to another part of the parent application without first responding to the dialog box. A modeless dialog box does not prevent the parent program from running which means it does not demand a response before input can be re-focused to another part of the program. but now the question comes how does Windows make use of its WndProc to achieve this kind of Dialogs. See the details below
What Windows does when you do a DoModal is overwrite the WndProc of the calling procedure with the one of the Modal Dialog and all INPUT type messages are blocked by the Windows Default implementation of Modal Dialog and now passed on to the calling WndProc and thus we get the Modal dialogs. i.e. No function works until Modal Dialog is destroyed.
Looks pretty simple :D . Try it out. I have already tried and manage to succeed to a great level but not perfect and so would not share the code here.
Now after reading the above para anybody would have made the guess how this works and yes you are right. Both the WndProc runs side by side. When starting with MFC its always easy to get the Modal Dialog up and running faster than Modeless ones but behind the scene implementation as it looks now is vice versa.
Re: Modeless Dialogs in MFC
|All times are GMT +5.5. The time now is 18:00.|