Exposing Autoclik’s m_pt Indirectly

In the following procedure, you will expose m_pt indirectly as a property and add code to implement it, while ClassWizard generates the stub handlers for it. ClassWizard enables you to implement methods and properties the same way you implement member functions.

To indirectly expose the m_pt 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 x.

  6. In the Type list box, select short.

  7. Under Implementation, select Get/Set methods.

  8. Click OK.

    This returns you to the Automation tab. The new property, listed as x in the Name list, is implemented with Get and Set member functions. The Implementation box shows:

    short GetX();
    void SetX(short nNewValue);
    

    The gray glyph with a “C” indicates that there is code associated with these member functions.

  9. Click the Edit Code button.

  10. Implement the Get method with the following code (replace the //TODO comment and existing code):
    return m_pt.x;
    
  11. Implement the Set method with the following code (replace the //TODO comment)::
    m_pt.x = nNewValue;
    Refresh();
    
  12. Repeat steps 1 through 11 for the y property, ending with:
    short CAutoClickDoc::GetY()
    {
    return m_pt.y;
    }
    
    void CAutoClickDoc::SetY(short nNewValue)
    {
    m_pt.y = nNewValue;
    Refresh();
    }
    

    Note   The x and y members of a point are declared as long in Win32. This may generate the following warning when you build the project:

    warning C4244: 'return' : conversion from 'long' to 'short', possible loss of data

    You can disregard the warning; it will not affect how Autoclik runs.

ClassView displays the new methods. You can examine AutoClickDoc.cpp to see how ClassWizard updated the dispatch map of the document class:

BEGIN_DISPATCH_MAP(CAutoClickDoc, CDocument)
   //{{AFX_DISPATCH_MAP(CAutoClickDoc)
   DISP_PROPERTY_EX(CAutoClickDoc, "x", GetX, SetX, VT_I2)
   DISP_PROPERTY_EX(CAutoClickDoc, "y", GetY, SetY, VT_I2)
   //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

You can see how the information you entered in ClassWizard is reflected in the dispatch map.