Windows dialog box class




















If the entire caption text is not showing, use the resize handle to extend the control to the right. Click OK. The Push Button Properties sheet will appear automatically and your typing will be entered in the Caption box. Do not change the control ID.

Close the Push Button Properties sheet. Reorganize the Connect button and the Cancel button as shown in Figure 4. Often, users need to be able to select values from some controls in a dialog box and alter values in others. For example, they might need to make a selection from a list box or enter text in a text box. They usually need to make a selection between two command buttons. To facilitate this movement, the controls need to be selectable.

When a particular control is selected, it is said to have the focus. When a control has the focus, the user is able to interact with that control. The control with the focus is readily apparent. It looks different from the other controls because it appears with an outline or contains the insertion point or is distinguished in some other way.

This provides a clear signal about which control will be affected by the user's actions. A user can navigate through the controls displayed on a dialog box by using the TAB key.

When the user presses the TAB key, the focus changes from one control to the next. Not all of the controls on a dialog box can necessarily receive the focus. For example, a static text control cannot. But each control capable of receiving the focus gets a turn. The order in which their turns come is known as the tab order. The tab order of the controls in a dialog box is set by the developer.

The tab order provides a means for navigating among the controls using only the keyboard. This order can be an important aspect of a heavily used application, where the thousands of times a month a user might need to move a hand from the keyboard to manipulate the mouse can add up to lost productivity and increased fatigue. Pressing the TAB key moves forward through the tab order. When a dialog box is initially displayed, the first selectable control receives the focus.

On a dialog box with numerous controls, the user may need to press the TAB key many times to reach the control of interest. Access keys are handy in these cases. To provide an access key for a selectable control, place a static text control in the tab order immediately prior to the control to which it is to be assigned and include an ampersand in the displayed text for the static text control.

When the user presses the access key for the static text control, focus goes to the first selectable control in the tab order after the static text control to which that access key was assigned. Numbers representing the current tab order will appear in the upper left corner of each control in the dialog box.

Click the Data Source caption, then the list box control, then each of the input box captions, and then each of the associated input boxes. Next, select the Attempt to connect at application startup check box, then click Connect , and finally click Cancel. The order in which you click determines the tab order. Try testing the tab order. Test the access keys to ensure the tab order was set correctly to permit moving among controls using the access keys.

Try selecting the different sample commands in the list box, and then try closing the dialog box with the Continue or Cancel button. Once you have created a dialog box template, you need to create a class to represent the dialog box in your code. This class might contain member variables to represent controls and data items in the dialog box, and methods to handle events arising from user interaction with the controls. The task of creating a dialog box class, and the binding of controls to class data members, is greatly simplified by automatic features of ClassWizard.

In this lesson, you will use ClassWizard to create a class for your Connect to Data Source dialog box and you will learn how to display the dialog box in your application. You will learn how to work with dialog data and with dialog box controls in Chapter 5. You will see the Adding a Class dialog box shown in Figure 4. With the Create a new class option selected, click OK. The New Class dialog box appears. In the Name text box, type CConnectDialog. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.

Privacy policy. You use dialog boxes to display information and prompt for input from the user. Your application loads and initializes the dialog box, processes user input, and destroys the dialog box when the user finishes the task.

The process for handling dialog boxes varies, depending on whether the dialog box is modal or modeless. A modal dialog box requires the user to close the dialog box before activating another window in the application.

However, the user can activate windows in different applications. A modeless dialog box does not require an immediate response from the user. It is similar to a main window containing controls. The simplest form of modal dialog box is the message box. Most applications use message boxes to warn the user of errors and to prompt for directions on how to proceed after an error has occurred. You create a message box by using the MessageBox or MessageBoxEx function, specifying the message and the number and type of buttons to display.

The system creates a modal dialog box, providing its own dialog box template and procedure. After the user closes the message box, MessageBox or MessageBoxEx returns a value identifying the button chosen by the user to close the message box. In the following example, the application displays a message box that prompts the user for an action after an error condition has occurred.

The message box displays the message that describes the error condition and how to resolve it. You create a modal dialog box by using the DialogBox function.

You must specify the identifier or name of a dialog box template resource and a pointer to the dialog box procedure. The DialogBox function loads the template, displays the dialog box, and processes all user input until the user closes the dialog box. In the following example, the application displays a modal dialog box when the user clicks Delete Item from an application menu. A dialog box, like any other window, receives messages from Windows.

In a dialog box, you are particularly interested in handling notification messages from the dialog box's controls since that is how the user interacts with your dialog box. Use the Class Wizard to select which messages you wish to handle and it will add the appropriate message-map entries and message-handler member functions to the class for you. You only need to write application-specific code in the handler member functions.

In all but the most trivial dialog box, you add member variables to your derived dialog class to store data entered in the dialog box's controls by the user or to display data for the user.

You can use the Add Variable wizard to create member variables and associate them with controls. At the same time, you choose a variable type and permissible range of values for each variable. The code wizard adds the member variables to your derived dialog class. A data map is generated to automatically handle the exchange of data between the member variables and the dialog box's controls. The data map provides functions that initialize the controls in the dialog box with the proper values, retrieve the data, and validate the data.

To create a modal dialog box, construct an object on the stack using the constructor for your derived dialog class and then call DoModal to create the dialog window and its controls. If you wish to create a modeless dialog, call Create in the constructor of your dialog class.

The exchange and validation data map is written in an override of CWnd::DoDataExchange that is added to your new dialog class. See the DoDataExchange member function in CWnd for more on the exchange and validation functionality.

The framework calls UpdateData when the user clicks the OK button to close a modal dialog box. The data is not retrieved if the Cancel button is clicked. The default implementation of OnInitDialog also calls UpdateData to set the initial values of the controls. You typically override OnInitDialog to further initialize controls. OnInitDialog is called after all the dialog controls are created and just before the dialog box is displayed. You can call CWnd::UpdateData at any time during the execution of a modal or modeless dialog box.

If you develop a dialog box by hand, you add the necessary member variables to the derived dialog-box class yourself, and you add member functions to set or get these values.

A modal dialog box closes automatically when the user presses the OK or Cancel buttons or when your code calls the EndDialog member function. When you implement a modeless dialog box, always override the OnCancel member function and call DestroyWindow from within it.

Don't call the base class CDialog::OnCancel , because it calls EndDialog , which will make the dialog box invisible but will not destroy it. You should also override PostNcDestroy for modeless dialog boxes in order to delete this , since modeless dialog boxes are usually allocated with new.

Modal dialog boxes are usually constructed on the frame and do not need PostNcDestroy cleanup. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science.

Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.



0コメント

  • 1000 / 1000