Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows


The Find dialog
box of WordPad is
an example of a
modeless dialog
box. Although it is
displaying on this
picture, the user
can still click and
activate any of the
windows in the
background


Because a modeless dialog box is closed when the user judges it necessary, making it
possible to forget, the operating system needs to make sure that this object closes when
its parent application closes so its memory resource can be freed and made available to
other applications. Based on this, the creation of a modeless dialog is a little different
than that of the modal dialog box.

There are two main techniques you can use to create a modeless dialog box. You can first
declare a DLGTEMPLATE variable and “fill it up”. This structure is defined as follows:

typedef struct {
DWORD style;
DWORD dwExtendedStyle;
WORD cdit;
short x;
short y;
short cx;
short cy;
} DLGTEMPLATE, *LPDLGTEMPLATE;

After building the template from its variable, you can call the CDialog::CreateIndirect()
method to create the modeless object. This method comes in two versions as follows:

BOOL CreateIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd = NULL);
BOOL CreateIndirect(HGLOBAL hDialogTemplate, CWnd* pParentWnd = NULL);

Because of the amount of work involved with using this type of dialog box, we will
ignore it.

Another technique you can use consists of first creating a dialog resource and associating
a class to it. To formally create a modeless dialog box and make it part of the application,
you can call the Create() method of the CDialog class. It is provided in two syntaxes that
are:

BOOL Create(UINT nIDTemplate, CWnd* pParentWnd = NULL);
BOOL Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
Free download pdf