In this topic, you will use WizardBar to add the member function ReplacePen
.
To create the ReplacePen helper function
The Add Member Function dialog box appears.
void
).In this case, type the following:
ReplacePen ()
You don’t need to type a semicolon. Also, for functions such as this that take no parameters, the parentheses are optional.
ClassWizard adds the declaration to the beginning of the first Protected section of the header file it finds (creating the section if it does not exist); creates a starter definition in the implementation file; and jumps you to the declaration.
ReplacePen
from the Members combo box. ReplacePen
: m_nPenWidth = m_bThickPen ? m_nThickWidth : m_nThinWidth;
// Change the current pen to reflect the new width.
m_penCur.DeleteObject( );
m_penCur.CreatePen( PS_SOLID, m_nPenWidth, RGB(0,0,0) );
The ReplacePen
member function uses the C conditional operator (?:) to determine the pen width and return its value. Then it calls the DeleteObject
member function of the current pen object and creates a new solid black pen with CreatePen
, setting its width and other attributes.
Now that you’ve created ReplacePen
, you want to call it from the Scribble code that initializes the pen width. This happens in the InitDocument
member function code.
To incorporate ReplacePen into Scribble
CScribbleDoc
in the Class combo box and InitDocument
in the Members combo box. Click the action button to jump to InitDocument
.ReplacePen
:ReplacePen(); // Initialize pen according to current width
Note This code replaces the code you originally added in Lesson 4, Creating the View:
m_nPenWidth = 2; // Default 2 pixel pen width
// Solid, black pen
m_penCur.CreatePen( PS_SOLID, m_nPenWidth, RGB( 0,0,0 ) );
because this code was incorporated into the ReplacePen
function.