Update Scribble’s Thick Line Menu Item

Updating the Thick Line menu item is similar to updating the Clear All menu. In this case, however, rather than enabling or disabling the menu item, the handler puts a check mark beside the item or removes an existing check mark.

To add an update handler for the Thick Line menu item

  1. In the WizardBar Filter combo box, select ID_PEN_THICK_OR_THIN.

  2. Click the action button arrow on the right end of WizardBar and click Add Windows Message Handler.

  3. In the New Windows messages to handle box, select UPDATE_COMMAND_UI. Click Add and Edit.

    The Add Member Function dialog box appears, displaying a suggested name for the handler.

  4. Click OK in the Add Member Function dialog box to accept the name OnUpdatePenThickOrThin, and to create the handler.

  5. Replace the //TODO comments with the following code:

    // Add check mark to Pen Thick Line menu item if the current
    // pen width is "thick."
    pCmdUI->SetCheck( m_bThickPen );
    
  6. Save your work.

Rather than enabling or disabling the menu command, this handler uses the pointer to a CCmdUI object to call the SetCheck member function. SetCheck puts a check mark in front of a menu item if its argument evaluates to TRUE and removes the check if the argument evaluates to FALSE. In this case, the expression m_bThickPen is a member variable of CScribbleDoc. It evaluates TRUE if the line thickness is currently set to thick. Since the value of m_bThickPen is passed to SetCheck, the effect is to toggle the menu item’s check mark on or off as the line thickness changes.

The ON_UPDATE_COMMAND_UI message-map entry and the OnUpdatePenThickOrThin message handler serve to update the state of the Thick Line button on the toolbar as well as the Thick Line menu item. The code line

pCmdUI->SetCheck( m_bThickPen );

adjusts the state of the toolbar button as well as updates the checked state of the menu item. For a toolbar button, “checked” means depressed.

In this example, the user would previously have reset the line thickness. The next time the user clicks the Pen menu item (or the toolbar button), the user-interface update mechanism takes care of updating the check mark to match the current thickness. Similarly, the toolbar button’s state toggles between a “pressed down” appearance and a normal appearance.

As with the update handler for Clear All, ClassWizard adds a message-map entry for OnUpdatePenThickOrThin to the document’s message map in ScribbleDoc.cpp:

BEGIN_MESSAGE_MAP( CScribbleDoc, CDocument )
   //{{AFX_MSG_MAP(CScribbleDoc)
   // Other message-map entries
   ON_UPDATE_COMMAND_UI(ID_PEN_THICK_OR_THIN,OnUpdatePenThickOrThin)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP( )

ClassWizard also adds a member function declaration to the document class definition in ScribbleDoc.h:

afx_msg void OnUpdatePenThickOrThin( CCmdUI* pCmdUI );