Dialog Boxes and Property Sheets Kevin Devaney Dialog
Dialog Boxes and Property Sheets Kevin Devaney
Dialog Boxes • Modal – Owner window disabled until dialog closed • Modeless – Owner window can be reactivated while dialog is still open • Property Sheets – Dialog boxes with tabbed pages
Class Hierarchy
Example Dialog Box • Font dialog • Controls – Combo boxes – List boxes – Check boxes – Push buttons
Create a Simple Dialog App • Use MFC App. Wizard – Visual C++ Menu • File / New / MFC App. Wizard – “Step 0” - Define project name and location – Step 1 – Choose “Dialog-based” – Other Steps – Accept defaults
Using MFC App. Wizard
Add Dialog Controls • Use toolbar to select controls for your dialog – Click on the control you want – Click on the dialog where you want to put the control – Drag mouse on edges of the control to resize it – Right-click on control, select properties and provide new ID
Dialog Resource File (. rc) IDD_CREDIT_DIALOGEX 0, 0, 265, 154 STYLE DS_MODALFRAME | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_APPWINDOW CAPTION "Credit Card Calculator" FONT 8, "MS Sans Serif" BEGIN DEFPUSHBUTTON "Calculate", IDOK, 198, 102, 41, 14 PUSHBUTTON "Exit", IDCANCEL, 198, 126, 41, 14 CONTROL "Spin 1", IDC_SPIN_DEBT, "msctls_updown 32", UDS_ARROWKEYS, 21, 22, 11, 14 END Resource file has “dialog template” Dialog box characteristics (popup) Types of controls used in the dialog (spin) Control position, size, characteristics
Adding Shortcut Keys Right-click on control and select Properties &Exit Alt-E will activate the Exit button
Setting Tab Order In Visual C++ menu, choose Layout/Tab Order Current tab order will be shown Click on controls in desired order to set a new tab order
Responding to User Input • Double-click on the control • “Add Member Function” dialog will pop up • Select “OK” and it will create a function for that control – On. OK for OK button – On. Change. Edit. Xxxx for edit control • Add code to the function to perform the needed processing
Temperature Converter IDC_EDIT_INPUT IDC_EDIT_OUTPUT
On. OK Processing
How to talk to controls - 1 • Use CWnd functions – Get. Dlg. Item. Text, Set. Dlg. Item. Text – Check. Radio. Button, Get. Checked. Radio. Button – Check. Dlg. Button, Is. Dlg. Button. Checked • This was illustrated in Temperature Converter example
How to talk to controls - 2 • Use info supplied in the message handler
How to talk to controls - 3 • Use common control classes – CEdit, CList. Box, CCombo. Box, CSlider. Ctrl, etc • Use CWnd Attach function to associated control class with your control
Common Control Class Example Remember to use Detach function if you create the object on the stack
How to talk to controls - 4 • Use Dynamic Data Exchange • Override virtual function Do. Data. Exchange • Use DDX functions to associate member variables with controls • Exchange occurs: Input (variable to control) in On. Init. Dialog Output (control to variable) in On. OK
DDX Example
DDX Functions DDX_Text DDX_Check DDX_Radio DDX_LBIndex, DDX_LBString DDX_CBIndex, DDX_CBString DDX_Scroll Edit control Check box Radio box List box Combo box Scroll bar
Dynamic Data Validation • Dynamic data validation (DDV) works similar to DDX • DDV checks that input data is within specified limits • If data is outside limits, focus is passed to the control and an error message is displayed
DDV Example
DDV Functions DDV_Min. Max. Byte, DDV_Min. Max. Int, DDV_Min. Max. Long, DDV_Min. Max. UInt, DDV_Min. Max. DWord, DDV_Min. Max. Float, DDV_Min. Max. Double, DDV_Max. Chars
Modal vs Modeless • Modal – Display dialog by calling CDialog: : Do. Modal – Exit by calling CDialog: : On. OK or On. Cancel – Usually instantiated on the stack so destruction is automatic
Modal vs Modeless • Modeless – Display dialog by calling CDialog: : Create – Exit dialog by calling Destroy. Window • Don’t call On. OK or On. Cancel – Usually instantiated on the heap (new) so the dialog is not destroyed prematurely • Make sure dialog is deleted by overriding CDialog: : Post. Nc. Destroy function using “delete this” statement
Basic Modal Dialog Programming (see example p. 398) • Declare dialog member variables in header file (. h) • Initialize variables and setup controls in On. Init. Dialog • Use Class Wizard to create message handler functions that get user input • Perform data processing in On. OK and comment out call to Cdialog: : On. OK()
Property Sheets • Tabbed dialog boxes • User can switch between pages using mouse • MFC classes – CProperty. Sheet – CProperty. Page
Common Dialogs CFile. Dialog CPrint. Dialog CPage. Setup. Dialog CFind. Replace. Dialog CColor. Dialog CFont. Dialog
CFile. Dialog
CFile. Dialog code TCHAR sz. Filters[] = _T("Text files (*. txt)|*. txt|All files (*. *)|*. *||") CFile. Dialog dlg(TRUE, _T("txt"), _T("*. txt"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, sz. Filters); if( dlg. Do. Modal() == IDOK ) { filename = dlg. Get. Path. Name(); }
- Slides: 30