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, 200This 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_SYSMENUThe 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:
- DS_MODALFRAME: Our dialog box can be modal
- WS_VISIBLE: Our dialog resource will be visible when it is loaded
- WS_CHILD: Our dialog box can have a parent window
- 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.
Next: Encapsulating our dialog resource >>
More C++ Articles
More By Mitchell Harper