Declaring the CPenWidthsDlg Object
In the following procedure you’ll use WizardBar to add a function handler for the OnPenWidths
message and bind the function to its handler code, which is executed whenever the user clicks the Pen Widths command.
To declare the CPenWidthsDlg object
Open ScribbleDoc.cpp in the text editor.
Click the arrow on the action button on the right end of WizardBar.
Click Add Windows Message Handler.
The New Windows Message Handler dialog box appears.
In the Class or object to handle list box, select ID_PEN_WIDTHS.
In the New Windows messages to handle list box, select COMMAND.
Click Add and Edit.
In the Add Member Function dialog box, click OK to accept the candidate name “OnPenWidths.”
In place of the highlighted //TODO comment, add the following code:
CPenWidthsDlg dlg; // Initialize dialog data dlg.m_nThinWidth = m_nThinWidth; dlg.m_nThickWidth = m_nThickWidth; // Invoke the dialog box if (dlg.DoModal() == IDOK) { // retrieve the dialog data m_nThinWidth = dlg.m_nThinWidth; m_nThickWidth = dlg.m_nThickWidth; // Update the pen used by views when drawing new strokes // to reflect the new pen widths for "thick" and "thin". ReplacePen(); }
Scroll to the top of ScribbleDoc.cpp and add the following #include statement:
#include "PenWidthsDlg.h"
Save ScribbleDoc.cpp.
When you modify ScribbleDoc.cpp, you must include PenWidthsDlg.h so the message handler has access to the dialog class you’ve created.
The OnPenWidths
function:
Declares a
CPenWidthsDlg
object and sets the values of them_nThickWidth
andm_nThinWidth
member variables to the current widths of the thick and thin pens.Calls the DoModal function, which displays the dialog box on the screen and takes control of the application until the user exits the dialog box. If the user exits the dialog box by clicking the OK button, the function changes the current thick and thin pen widths to the new values; if the user clicks the Cancel button, the old values are retained.
Calls the
ReplacePen
member function to make the document’s pen use the current widths.
When does the application perform the data exchange and validation defined in the DoDataExchange
function? Recall that DoDataExchange
is called by the UpdateData member function. Just before the dialog box is first displayed on the screen, the framework calls the UpdateData function with an argument of FALSE, which sets the contents of the edit boxes to the values of the member variables. If the user exits the dialog box by clicking the OK button, the framework calls UpdateData with an argument of TRUE, which retrieves the contents of the edit boxes and sets the values of the member variables accordingly. (If the user exits by clicking the Cancel button, the framework doesn’t call UpdateData.)
You don’t have to handle the UPDATE_COMMAND_UI message for the Pen Widths menu item because the menu item doesn’t need to be updated. The command is never disabled since it’s always legal to change the widths of the pens, and there’s no need to add or remove a check mark because the command isn’t a toggle.