COMPLETING DIALOG OPERATIONS

You must now modify the OnPenWidth() handler in CSketcherDoc to make the dialog effective. Add the following code to the function:

// Handler for the pen width menu item
void CSketcherDoc::OnPenWidth()
{
  CPenDialog aDlg;                   // Create a local dialog object
 
  // Set pen width in the dialog to that in the document
  aDlg.m_PenWidth = m_PenWidth;
  
  if(aDlg.DoModal() == IDOK)         // Display the dialog as modal
    m_PenWidth = aDlg.m_PenWidth;    // When closed with OK, get the pen width
}

The m_PenWidth member of the aDlg object is set to the pen width stored in the m_PenWidth member of the document; you still have to add this member to CSketcherDoc. The DoModal() function call now occurs in the if statement condition, which is true if the DoModal() function returns IDOK. In this case you retrieve the pen width stored in the aDlg object and store it in the m_PenWidth member of the document. If the dialog is closed by the Cancel button, the keyboard escape key, or the close icon, IDOK won’t be returned by DoModal() and the value of m_PenWidth in the document object will not be changed.

Note that even though the dialog is closed when DoModal() returns a value, the aDlg object still exists, so you can call its member functions. The aDlg object is destroyed on return from OnPenWidth().

All that remains to do to support variable pen widths in your application is to update the affected classes: CSketcherDoc, CElement, and the four element classes derived from CElement.

Adding Pen ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.