Home arrow C++ arrow Page 3 - 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 (contd.)
(Page 3 of 7 )

Because our dialog box is a resource, we will add it to the bottom of our mydialog.rc file, as shown below:

TestDialog DIALOG 0, 0, 300, 200

CAPTION "My Test Dialog Box"

STYLE DS_MODALFRAME | WS_VISIBLE | WS_CAPTION | WS_SYSMENU

{

// Controls for the dialog will go here

}


We have just created a new dialog resource. Our new dialog resource has three components, as described below:

TestDialog DIALOG 0, 0, 300, 200

This is the declaration of our dialog resource. It contains six properties:

[DialogId], DIALOG, [xPos], [yPos], [width], [height]

DialogId: The id that we will use to reference our dialog box. Can be either a string of text, or a #defined ID. We have used TestDialog, which is interpreted by C++ a string of text, because we haven’t #defined an ID named TestDialog.

DIALOG: The dialog keyword tells the C++ resource compiler that we are creating a new dialog box. If we wanted to create a dialog box with extended styles, we would use DIALOGEX instead.

xPos: The x-coordinate from which our dialog box will be positioned when it is loaded. This co-ordinate is relative to the parent window of the dialog box (if any).

yPos: The y-coordinate from which our dialog box will be positioned when it is loaded. This co-ordinate is relative to the parent window of the dialog box (if any).

width: The width of the dialog resource, in logical units (pixels).

height: The height of the dialog resource in logical units (pixels).

CAPTION "My Test Dialog Box"

This line simply sets the window caption of our dialog resource.

STYLE DS_MODALFRAME | WS_VISIBLE | WS_CAPTION | WS_SYSMENU

The third line of our dialog resource defines how it will look. Firstly, we have the keyword STYLE. STYLE is suffixed with four different MFC defined style macros:
  1. DS_MODALFRAME: Our dialog box can be modal
  2. WS_VISIBLE: Our dialog resource will be visible when it is loaded
  3. WS_CHILD: Our dialog box can have a parent window
  4. WS_SYSMENU: Our dialog box will have a system menu. A system menu simply means that when the icon in the title bar of the dialog resource is clicked, several menu items such as Restore, Minimize and Close will be displayed.
{

// Controls on the dialog will go here

}


Lastly, we have the “contents” of our dialog resource. As you can see, ours is empty. We will place resources for radio buttons, edit boxes, check boxes, etc in our dialogs contents soon. Instead of using opening and closing braces, we could use BEGIN and END respectively, however, I think that braces make our resource file easier to read.

We have just created our dialog resource. To actually make our dialog resource visible through our application, we have to encapsulate it within a class. We will do that now.
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