In part three of this ten part series, Mitchell talks about modal and modeless dialog boxes. "To create a fully interactive user experience, any application must employ some sort of dialog-based interaction. Dialog boxes come in two flavors: modal and modeless..."
Using MFC in C++ Part 3: Dialog Boxes - Creating a modal dialog box (Page 2 of 7 )
Modal dialog boxes are a common way to allow users of your application to interact with it. They are modal, which means that they remain in-focus during the whole time that they exist, and any other part of your application, such as its main window or another dialog box cannot be interacted with until the modal dialog box is closed first.
Modal dialog boxes are ideal when you need to get some sort of information from a user before allowing them to continue. Take for example, if you were developing a database application. The first time that the user loaded your application, they would need to enter some details about their database and the user credentials needed to connect to it. Using a modal dialog box, we would design our application so that all of the information required in the dialog box MUST be completed before the user can continue, or even close the dialog box.
To create a modal dialog box, we must first create the dialog box as part of a resource file. As described in article 2, you can create add a new resource file to your project by clicking on the File menu, selecting the New option, and then selecting either a resource file (To create the resource components using point-and-click), or a text file with a .rc extension (To create the resource components manually, using a resource script). We will create our modal dialog box manually.
We will start by creating the framework of our application. Create a new empty Win32 application named MyDialog. Add a new class to MyDialog named main. This will add two files to your project: main.h and main.cpp. Next, use the File…New menu option to add a text file to your project. Also, add a single header file, ids.h. Name the text file mydialog.rc.
Fill main.h with the following code:
class CMainWin : public CFrameWnd
// Show dialog function
afx_msg void OnShowDialog();
class CApp : public CWinApp
Fill main.cpp with the following code:
Create(NULL, "My Dialog Application", WS_OVERLAPPEDWINDOW,
rectDefault, NULL, "MainMenu");
MessageBox("Couldn't load accelerator table");
m_pMainWnd = new CMainWin;
afx_msg void CMainWin::OnShowDialog()
Next, we will refresh our memory on menus from the second article, by adding one to our application. Double click on mydialog.rc and enter the following code:
MENUITEM "&Show Dialog\tCtrl+S", IDM_SHOWDIALOG
"s", IDM_SHOWDIALOG, CONTROL, VIRTKEY
Lastly, enter the following code into ids.h:
#define IDM_SHOWDIALOG 101
If any of this code is unfamiliar to you, don’t worry. It’s all explained in article 1 and article 2 of this series. We have just created a main window, with a menu. The menu has one popup, with one item, “Show Dialog”. We have also created an accelerator table, with one key, Ctrl+S, which is linked to the same function as the “Show Dialog” menu item.