Page 200 - DCAP408_WEB_PROGRAMMING
P. 200
Windows Programming
Notes
Did u know? Because once the dialog box has been dismissed, all of its local variables are
destroyed and no longer accessible
Ooh, there were some ugly words in that last paragraph. “Global variables”? Well, we did what
we had to do to get our applications out the door on time. But today’s Windows programmer can
use the power of C++ and the MFC. Let’s now think in terms of C++, then add some MFC
concepts as they relate to a dialog box. In C++, when you instantiate an object from a class, the
object exists until you specifically destroy it, either manually or by leaving your own current
scope. So unlike a typical ‘C’ subroutine, our C++ class may have as many member functions
(subroutines) and member variables (“global” to that class) as we like. Best of all, we can keep
the object derived from the class around for as long as we need it. Now, suppose that for a
minute we developed a class using MFC designed to display a dialog box. We could tell the
object derived from the class to display itself, and when the user dismissed the dialog box, the
C++ object would still exist, as we have not taken action to destroy it.
Notes MFC classes “contain” windows, or window handles, as if the class were a bottle…the
class “holds” the window handle but is not the window itself.
So far, then, we know we can create a class to display a dialog box. Any class we create may
contain member variables which we are free to initialize (assuming they are created as public).
So, we build this dialog class, add some public member variables, and go to town. Right? Well,
not quite yet. The problem we have is the dialog box, as a window, contains controls such as edit
boxes, static text, radio buttons, etc. The class we instantiated merely holds a window handle,
and even that is invalid until the dialog box itself is created. So how do we fill the controls inside
the dialog box before it’s created? We do so by using DDX.
Our first task on the road to using DDX is to create a main application, then create a new dialog
box class. Assuming you’ve built your basic application (another topic, another day), you first
“lay out” your dialog box using the dialog box editor (or resource editor, if you prefer) in Visual
C++.
Example: Let’s insert a new dialog box, IDD_MYDIALOG. Then, include a text edit box
we’ll call IDC_EDITBOX and a check box we’ll call IDC_CHECKBOX (for the check box text, use
“Check Me!”). Feel free to arrange these controls and give the dialog box any style or
ornamentation you wish.
Now, activate the Class Wizard by selecting it under the “View” menu. We’ll use the “Add
Class” button under the “Message Map” tab—press this button and select “New”. This will
activate another dialog box, where we’ll call our new class “DemoDialog” (under “Name”) and
give it a base class of CDialog (under “Base Class”). You should see the dialog identifier
IDD_MYDIALOG “Dialog ID”.
!
Caution To be sure Class Wizard saves your work to date, click OK twice. This will exit
Class Wizard and store some new files for you…these are your dialog box class files.
Again activate Class Wizard, only this time select the “Member Variables” tab. Do you see your
control identifiers, IDC_EDITBOXand IDC_CHECKBOX? You should. IDC_CHECKBOX will be
194 LOVELY PROFESSIONAL UNIVERSITY