Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Dialogs and Controls

Similar presentations


Presentation on theme: "Working with Dialogs and Controls"— Presentation transcript:

1 Working with Dialogs and Controls
CITA 342 Section 11 Working with Dialogs and Controls

2 Modal Dialog Boxes Modal dialog boxes require users to close or cancel the dialog box before they can continue working with an application. Once a modal message box appears on your screen, you cannot access any other window in an application until you close the message box.

3 Modeless Dialog Boxes In comparison to a modal dialog box, modeless dialog boxes do not need to be closed before you return to another window in the application. Modeless dialog boxes function more like frame windows and other types of primary application windows. Modeless dialog boxes require quite a bit more work than modal dialog boxes.

4 Dialog Boxes Much of the behind-the-scenes work, such as closing and destroying the dialog window, is handled automatically with modal dialog boxes. With modeless dialog boxes, however, especially in dialog-based applications, you need to override several inherited member functions. Overriding the inherited member functions allows you to correctly close and destroy the dialog window.

5 Displaying Modal Dialog Boxes
You display a modal dialog box from an application’s InitInstance() function, the same way you display a frame window. You instantiate an object of the dialog class and use the inherited DoModal() function to display the modal dialog box. By default, if a user clicks a button containing a resource ID of IDOK or IDCANCEL, the dialog box closes.

6 Displaying Modal Dialog Boxes
The IDOK resource ID represents the OK button, and the IDCANCEL resource ID represents the Cancel button. The DoModal() function returns an integer value representing the resource ID that caused the dialog box to close. You use these resource IDs in an if statement to take the appropriate action, depending on whether the user pressed the OK button or the Cancel button.

7 Displaying Modeless Dialog Boxes
Construct using the resource editor in the same way as a modal dialog. Launch it by calling CDialog::Create(). instead of CDialog::DoModal(). Dismiss it by calling CWnd::DestroyWindow(). instead of CDialog::EndDialog(). you must NEVER call EndDialog() for a modeless dialog. you MUST override OnCancel() in order to prevent it from calling EndDialog().

8 Working with Controls Dialog boxes typically contain groups of controls through which a user interacts with an application. Controls are user interface items such check boxes, command buttons, text boxes, and other objects. You add controls to a dialog box by using the Controls toolbar in the Dialog Editor.

9 Dialog Box All dialog boxes—whether standard or custom—are created using two components: a dialog resource and a dialog class derived from the CDialog class. The dialog resource represents the visual aspect of the dialog box, and the dialog class provides programmatic access to the dialog box.

10 Dialog Data Exchange/Validation
MFC provides a special mechanism called dialog data exchange, or DDX, to handle the exchange of values between controls and variables. A related mechanism called dialog data validation, or DDV, assists in the validation of data as it is exchanged between controls and variables. Before you can use DDX or DDV, you must first override the DoDataExchange() function in the class derived from CDialog that is associated with your dialog resource ID.

11 MFC Controls Controls are the user interface objects used to create interfaces for Windows applications. Most Windows applications are nothing but a collection of controls arranged in a way that appropriately implements the functionality of the program. There are only six basic controls - CStatic, CButton, CListBox, CComboBox, CEdit, and CScrollBar - along with some minor variations (Windows added a collection of about 15 enhanced controls as well).

12 Control Examples

13 Control Classes Six basic MFC Control Classes:

14 Common Controls See

15 Dialog Editor Picture (bmp, mtf, icn, …) Static Text, Edit Box Group Box, Button Check Box, Radio Button Combo Box, List Box Scroll Bars Spin Control, Progress Slider, Hot-Key List, Tree Tab Control, Animation Rich Edit, Custom Controls are dragged onto the control with the mouse and some are also sized using the mouse - the resource file is created by the resource editor. Right click on a selected control to get a property sheet which can be used to set control styles.

16 Controls The Resource Editor makes it easy to add controls to dialog boxes. The controls are automatically created when the dialog is created. The Class Wizard makes it easy to link these controls to dialog class member functions or member variables.

17 Controls Controls are actually Windows Controls are Child Windows
They have their own window procedures (provided by Windows). In MFC, they inherit all the CWnd functionality. Controls are Child Windows clipped to parent, move with parent, destroyed with parent. when using VC++ Resource Editor - parent is usually a dialog class derived from CDialog. send WM_COMMAND notifications to parent.

18 CDialog Usually encapsulate your dialog box by a class derived from CDialog. To add member variables corresponding to the controls. Exception: simple dialogs with no data (e.g., About box).

19 CDialog Primary overridable (virtual) functions: OnInitDialog():
Override to initialize the dialog window or the controls. Call the base class version as well. OnOK(): Called when a button with the IDOK identifier is pressed. Do NOT need a message map entry for this. To extract & validate data from controls (unless DDX/DDV). Finish a modal dialog by calling base version or EndDialog() int passed to EndDialog() is returned to caller of DoModal().

20 CDialog Overridable (virtual) functions: OnCancel():
Called when a button with the IDCANCEL identifier is pressed, or when the ESC key or close button is pressed. Do NOT need a message map entry for this. Rarely overridden (except for modeless dialogs).

21 CDialog::DoModal() DoModal() shows the dialog window, and returns the value that the CDialog passes to EndDialog(). The default OnOK() calls EndDialog(IDOK.) The default OnCancel() calls EndDialog(IDCANCEL). You can call EndDialog() with other IDs if you wish.

22 DDX / DDV Basics Can use Class Wizard to create dialog data members and associate them with controls. Controls are “magically” initiated from the data members. OK “magically” sets the data members from the controls.

23 DDX / DDV Basics DoDataExchange() calls the appropriate DDX_ and DDV_ functions for each control. Pressing CANCEL (instead of OK) will simply not copy the data members to the View class variables.

24 DDX / DDV Basics You can force an exchange between the dialog member variables and the controls from anywhere in your dialog class by calling: UpdateData(TRUE): transfers from controls to variables. UpdateData(FALSE): transfers from variables to controls.

25 DDV Note that DDV data validation still doesn't happen until UpdateData() is called. most users would rather be told about an out-of-range selection BEFORE they hit the OK button. To do immediate range checking on most controls, you'll have to implement control message handlers.

26 Dialog as Main Window Popular approach for simple Apps:
primary I/O through controls, rather than graphics or text. document / view organization not justified. MSVC AppWizard gives you this option, but it will create a modal dialog as the main window.

27 Common Dialogs Standard implementations of commonly used dialog functions:


Download ppt "Working with Dialogs and Controls"

Similar presentations


Ads by Google