Windows Programming/Dialog Boxes: Modal
Windows Programming/Dialog Boxes: Modal
< Windows Programming
This page may need to be reviewed for quality.
People are familiar with dialog boxes. They are the grey windows that pop up
on Windows systems to display messages, and allow the user to set
parameters. There are 3 types of dialog boxes: modeless, modal, and system
modal.
Modal
Modal dialog boxes are generally used inside a program, to display
messages, and to set program parameters. Modal dialog boxes come to
the front of the screen, and you may not use the program while the
modal dialog box is open. to continue using the program, the modal
dialog box must be closed.
System Modal
System modal dialog boxes are like modal boxes, except that they
supersede the entire desktop area. When a system modal dialog box is
open, nothing else on the screen can be clicked or selected.
Modeless
Modeless dialog boxes are able to be deselected, and control can be
taken away from a modeless dialog box and transferred to some other
window. Modeless dialog boxes are frequently used as a fast and easy
way to create a window, without having to register a window class.
Modeless dialog boxes are common in the Windows control panel.
Contents
[hide]
1 MessageBox
o 1.1 Buttons
o 1.2 Icons
o 1.3 Modality
2 Dialog Box Procedures
3 Creating Modal Dialog Boxes
o 3.1 Indirect Dialog Boxes
4 Creating Modeless Dialog Boxes
5 Without Class
6 With Class
7 Common Dialog Boxes
o 7.1 ChooseColor
o 7.2 GetOpenFileName and
GetSaveFileName
o 7.3 ChooseFont
8 Dialog Box Resources
o 8.1 The DIALOG keyword
9 Next Chapter
[edit]MessageBox
[edit]Without Class
We can define a dialog box in a resource script with the DIALOG
keyword. The resource will have an ID associated with it (either a
number or a string), and this ID can be passed directly to the
CreateDialog function.
[edit]With Class
If we want to define a modeless dialog box in terms of a window
class, we can use a few additions to make the job easier. First, we
create a WNDCLASS structure with the information about our
dialog box. However, there is one difference, in that we must set
the cbWndExtra field to the value DLGWINDOWEXTRA value:
wnd.cbWndExtra = DLGWINDOWEXTRA;
Notice the field that says "CLASS"? This is the same string that
we used in our WNDCLASS structure to name the class. It is
important that these two strings be identical, because Windows
needs this string to link the WNDCLASS and the dialog box
resource together. Notice also that we used the string
"MYDLGCLASS" to identify the dialog resource. This isn't
mandatory, but it does make things convenient later on.
Now, instead of calling CreateWindow, we will call the easier-to-
use function CreateDialog. We do not use the DialogBox
function, because CreateDialog returns immediately, and doesn't
halt program execution.
Here is an example:
HWND hDlg;
hDlg = CreateDialog(hInst, "MyDlgClass",
hwndParent, MyDlgProc);
BOOL GetOpenFileName(LPOPENFILENAME
lpofn);
BOOL GetSaveFileName(LPOPENFILENAME
lpofn);
+---------------> X
["DialogBox" [_][O][x]]
+ | |
| | |
| | |
| | |
| | |
| | |
v | |
Y | |
| |
| |
+-----------------------+
After the declaration, you may optionally include one or more style
flags, to specify how you want a particular control to appear. The
WS_TABSTOP identifier specifies which controls can be selected
when you press the TAB key on the keyboard. When you press
the TAB key, control switches among the dialog box controls in
the same order that they are specified in the resource script (top
to bottom).