Dialog Boxes
Dialog Boxes
Dialog Boxes
Popup child windows created by Windows ? Used for special-purpose input & output
?
Contain several child window controls ? Layout & what it does is are predefined (template--a resource) ? How it does is determined by a "Dialog box procedure" ? Destroyed immediately after use
?
Modal
WM_INITDIALOG Message
?
? ?
Good place to put dialog box initialization code ? In an MFC CDialog-derived class, this message activates dialog boxs OnInitDialog() handler
EndDialog()
Destroys dialog box ? Returns control to function (WndProc()) that started the DialogBox()
?
message
Need to know control's handle Not known since Windows creates the controls IDs are known--specified in resource template
? ?
OnOK(), OnCancel()
Handlers for WM_COMMAND messages from OK and Cancel buttons Both call CDialogs EndDialog() function to dismiss the dialog box and return control to DoModal()
? 2.
Create a CDialog-based class ? 3. Instantiate a CDialog object ? 4. Call its DoModal( ) function
Dialog box msg handling done w/ message maps Dialog box class declarations (.h file):
Message map and handling function declarations
? 3.
Usually done in CView class in response to a main window menu item selection CMyDlg dlg;
Creates the dialog box (not activated yet) Initialization code, if any, should be put in CDialogs OnInitDialog() handler function
Invoked in response to WM_INITDIALOG message
? 4.
Use CDialogs DoModal() member function Displays the dialog box Messages from dialog box controls go to dialog box handler functions Continues until dialog box has been closed by user clicking OK or Cancel buttons
CDialogs EndDialog() member function causes DoModal() to return Can test return value
If(dlg.DoModal()==IDOK {//do something}
SetDlgItemText(IDC_CTRL, m_string);
Sends the string to the control
OK for non-Wizard-generated apps Theres a much easier way for Wizard-generated applications
Method 2
Use DDX (Dialog Data Exchange) mechanism Automatically built into Wizard-generated Apps DDX system moves data between dialog box controls and variables in Cdialog-derived class Occurs when a call is made to CWnd::UpdateData(direction); Boolean parameter sets direction of data movement
TRUE ? from controls to variables FALSE ? from variables to controls
Create a new Visual C++, MFC, SDI application (as usual) ? Add the sketching code (see earlier example) ? Add a new Text menu item (ID_TEXT) ? Add the new dialog box
?
Use the dialog box editor to drag over a static and an edit control:
Static Control: Text String Edit control: IDC_TEXTEDIT
? Create
Right click on an unoccupied area of the dialog box & choose Add Class to bring up the MFC Class Wizard Dialog Box Class name: CTextDlg Base class: CDialog
? Add
At top of Cview .cpp file underneath the other include statements, add: ? #include TextDlg.h
?