Creating the Tool Palette COM Wrapper

Once your basic project is defined, you need to add a COM object that can communicate with the Tool Palette framework. Since your project uses ATL, the easiest way to add COM objects is with Visual Studio ATL wizard. Consult the Microsoft documentation for guidance on using this wizard.

To create the COM wrapper

  1. Using the ATL wizard, create an ATL simple object. Make its short name “Simple Tool.” Accept defaults for the other names.
  2. Close the wizard dialog box to commit your selections.

    The ATL wizard creates several new files and adds them to your project. These files create an ISimpleTool interface definition and a CSimpleTool coclass.

  3. From the Visual Studio project view, open the SimpleTool.h file in the Visual Studio editor.

Note that the CSimpleTool coclass derives from the following standard ATL classes:

In the next procedure, you change this derivation to use the AcadToolImpl class.

To derive from the AcadToolImpl class

  1. In the SimpleTool.h file, include the AcadToolImpl.h file.
  2. Find the CSimpleTool coclass declaration.
  3. Remove the coclass' derivations from CComObjectRootEx and IDispatchImpl.

    CSimpleTool now derives only from CComCoClass.

  4. Add a new base template class, AcadToolImpl, after CComCoClass.
  5. Add the following arguments, in the order shown, to the AcadToolImpl template:
    • Coclass name (CSimpleTool)
    • Interface name (ISimpleTool)
    • A reference to the tool's class ID (CLSID_SimpleTool)

    These two additional LPCTSTR arguments also are required by the AcadToolImpl template:

    • The label that will identify the tool's icon on your tool palette
    • The icon's bitmap resource ID as a string

    In order to make this information available to both the declaration and the implementation, declare them as global variables.

  6. Near the top of the SimpleTool.cpp file, add the following non-static global declarations after the last #include statement:

    TCHAR szSimpleToolName[256] = “Simple Tool”;

    TCHAR szSimpleToolImage[256] = “IDB_TOOL1”;

  7. In the SimpleTool.h file, add the following extern declarations before the coclass declaration:

    extern TCHAR szSimpleToolName[256];

    extern TCHAR szSimpleToolImage[256];

    Declaring these globals in the implementation file and referencing them externally in the header file helps to avoid MSVC linker error LNK2005.

  8. Pass szSimpleToolName and szSimpleToolImage to the AcadToolImpl base class declaration as its fourth and fifth template arguments.

    The final coclass derivation should match the following:

    class ATL_NO_VTABLE CSimpleTool : 
        public CComCoClass<CSimpleTool, &CLSID_SimpleTool>,
        public AcadToolImpl<CSimpleTool, ISimpleTool,         &CLSID_SimpleTool, szSimpleToolName,         szSimpleToolImage>
    

    One final change must be made to ATL's default implementation. You do not need the ATL COM map when you derive from AcadToolImpl. The AcadToolImpl class handles outgoing interfaces.

  9. Delete the COM map macro declaration and its entries from the BEGIN_COM_MAP macro call to END_COM_MAP.

    Your coclass now is derived correctly, with unwanted ATL code removed. However, you need to make a few additions to support AcadToolImpl before you can compile and build the project.

    AcadToolImpl provides access to Property Inspector interfaces such as IAcPiPropertyDisplayImpl and IPerPropertyDisplay. This implementation makes it easy for you to adapt the Properties palette UI to your application's needs. Even if you do not intend to use this feature, however, you must provide a small amount of supporting code.

To finish the skeleton application

  1. In your CSimpleTool coclass declaration, add the following pair of macro calls:
    BEGIN_PERPROPDISPLAY_MAP2()
    END_PERPROPDISPLAY_MAP2()
    
  2. Add another pair of macro calls:

    BEGIN_FLYOUT_SPECIFIC_MAP()
    END_FLYOUT_SPECIFIC_MAP()
    
    Note: Though they are not used here, macro maps added in this procedure are required for compiling classes derived from the AcadToolImpl class.
  3. Add the following method definition to your coclass declaration:
    virtual HINSTANCE GetResourceInstance()
    {
        return _AtlBaseModule.GetResourceInstance();
    }
    
  4. In the SimpleTool.cpp file, include the AcTc_i.c file.