Windows Systems Programming: Spring 2002
[ Home |
Syllabus |
Course Notes |
Assignments |
Search]
Dialog Boxes
Although it is possible to use a dialog as
the main window of an application, most dialog boxes are child windows.
- Dialog boxes are used
- To give messages to the user
- Receive (usually) small amounts of information from the user
- Can be
- modal: wait till done, no other processing can proceed
- modeless: can work elsewhere in the application (find/replace
dialog)
this is more complicated than "modal" since need to manage several threads
of control and possible synchronize
- CDialog is an object derived from CWnd (see
hierarchy chart inside front cover.)
- Most dialog boxes are built using the dialog editor (one of the resource editors).
- Dialogs contain one or more elements called controls.
There is an excellent selection of controls already available which work together (radio
buttons, edit controls, sliders, lists, combo boxes, progress bars, etc)
More information on controls can be found here.
- Most controls are objects in MFC derived from CWnd.
- Challenge: how to communicate between the dialog box and the rest
of the application?
- How are the initial values of the control objects set?
- How to pass control back to the main application thread? For example, user clicks on a control (say the "OK" button)
the button is a window which gets a WM_LButtonDown message
- How are values set in a dialog transferred back to the main application?
- The answers form an interesting study of object oriented design and division of
responsibilities
Two critical questions any software designer should ask are:
- Which object should remember this information
(state)
- Which object is responsible for making this happen.
(action)
Three Simple Dialog
To illustrate some of the difficulties in building a good dialog box
and the advantage of reusing CDialog object from the MFC library and the use of the
Resource/Dialog Editor and Class Wizard, I present three versions of a simple dialog to
ask the user for a new title for the main window.
Version one does not
use CDialog, but builds the dialog in the main window itself. This is not a good solution
but it does clearly illustrate the communications between CEdit and CButton controls.
Version two uses CDialog
and the Dialog editor and illustrates how to incorporate a resource compiler generated
dialog into your program. Illustrates passing of data from Edit to MainWindow.
Version three uses the app
and class wizard and most closely follows the example in the book. Hopefully some the
magic going on behind the scenes will make sense.
Communication Solutions
-
Version 1: main window creates CEdit
control directly, uses "GetWindowText" to get new title which it
changes in itself
-
Version2: Dialog object uses "GetWindowText"
to get new title and stores in public data member. Main window creates local
dialog object and accesses public data member to change its title
-
Version3: Data Exchange functions move
data from CEdit to public data member of public dialog object. Main window
creates local dialog object and accesses public data member to change its
title. (added complication due to View object inside of main window.
Copyright chris wild 1999-2002.
For problems or questions regarding this web contact [Dr.
Wild].
Last updated: February 05, 2002.