Exposing Autoclik’s m_str Directly

In the following procedure, you will expose m_str directly as a property and study how ClassWizard adds it to the dispatch map. You’ll then remove the redundant declaration you added earlier. Finally, you’ll change the dispatch map declaration from private to public to give the view access to m_str.

To directly expose the m_str member variable in the dispatch interface

  1. From the View menu, click ClassWizard.

  2. Click the Automation tab.

  3. In the Class name box, select CAutoClickDoc, if it is not already selected.

  4. Click Add Property.

    The Add Property dialog box appears.

  5. In the External name box, type text1.

  6. In the Type list box, select CString.

  7. Under Implementation, select Member variable.

    Whereas you exposed m_pt indirectly by using the Get/Set methods option, expose m_str directly as a member variable.

  8. Replace ClassWizard’s proposed Variable name, m_text1 (which was based on the External name), with m_str.

    Soon you will see how to associate the text dispatch property with the m_str member variable already declared in the document class.

  9. Remove ClassWizard’s proposed notification function name, OnText1Changed.

    This step is included for instructional purposes. You could have implemented an OnText1Changed function by calling Refresh, just as you did for SetX and SetY. If you do not implement a similar OnText1Changed function, then you can see the different behavior when you drive AutoClik from an Automation client. When the Automation client updates the text, AutoClik does not automatically update its view as it does when the Automation client changes the x or y values. Instead, the Automation client must call the Refresh method to update AutoClik’s view with the most recently changed text.

  10. Click OK.

    This returns you to the Automation tab, which now displays the three properties: text, x, and y. The Implementation box for the text variable shows:

    CString m_str;
    
  11. Click OK.

  12. In ClassView, double-click CAutoClickDoc to open AutoClickDoc.h.

    ClassWizard has declared the following members in the dispatch map:

    //{{AFX_DISPATCH(CAutoClickDoc)
    CString m_str;
    afx_msg short GetX();
    afx_msg void SetX(short nNewValue);
    afx_msg short GetY();
    afx_msg void SetY(short nNewValue);
    //}}AFX_DISPATCH
    DECLARE_DISPATCH_MAP()
    

    At this point, the document header file declares m_str twice. The first declaration is the one you originally wrote:

    // Attributes
    public:
    CPoint m_pt;
    CString m_str;
    

    The second declaration is the one ClassWizard added above in the dispatch map.

  13. Remove the CString m_str declaration that you wrote, shown above.

  14. Add the public keyword just after the line:
    // Generated OLE dispatch map functions
    

    This changes the declaration of the dispatch map from protected to public. This is necessary because m_str had already been declared as public so it could be accessed by the view.