Home arrow C++ arrow Page 2 - Using MFC in C++ Part 3: Dialog Boxes
C++

Using MFC in C++ Part 3: Dialog Boxes


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..."

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 55
December 08, 2001
TABLE OF CONTENTS:
  1. · Using MFC in C++ Part 3: Dialog Boxes
  2. · Creating a modal dialog box
  3. · Creating a modal dialog box (contd.)
  4. · Encapsulating our dialog resource
  5. · Creating a modeless dialog box
  6. · The OnInitDialog() function
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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

{

public:

CMainWin();

// Show dialog function

afx_msg void OnShowDialog();

DECLARE_MESSAGE_MAP()

};

class CApp : public CWinApp

{

public:

BOOL InitInstance();

};


Fill main.cpp with the following code:

#include <afxwin.h>

#include "main.h"

#include "ids.h"

CMainWin::CMainWin()

{

Create(NULL, "My Dialog Application", WS_OVERLAPPEDWINDOW,

rectDefault, NULL, "MainMenu");

if(!LoadAccelTable("MainKeys"))

MessageBox("Couldn't load accelerator table");

}

BOOL CApp::InitInstance()

{

m_pMainWnd = new CMainWin;

m_pMainWnd->ShowWindow(m_nCmdShow);

m_pMainWnd->UpdateWindow();

return TRUE;

}

BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)

ON_COMMAND(IDM_SHOWDIALOG, OnShowDialog)

END_MESSAGE_MAP()

afx_msg void CMainWin::OnShowDialog()

{

}

CApp App;


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:

#include <afxres.h>

#include "ids.h"

MainMenu MENU

{

POPUP "&Dialog"

{

MENUITEM "&Show Dialog\tCtrl+S", IDM_SHOWDIALOG

}

}

MainKeys ACCELERATORS

{

"s", IDM_SHOWDIALOG, CONTROL, VIRTKEY

}


Lastly, enter the following code into ids.h:

#ifndef _IDS_H__

#define _IDS_H__

#define IDM_SHOWDIALOG 101

#endif


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.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials