The Ex29c Example: ATL and DHTML

ATL’s support for DHTML comes in the form of an HTML object that can be embedded in any ATL ActiveX control. Ex29c creates an ATL control that illustrates DHTML support.

To create the example, follow these steps:

  1. Run the ATL Project Wizard and create the Ex29c project.Select Executable as the server type on the Application Settings page.

  2. Insert an HTML control.From the Project menu, choose Add Class. Select ATL Control from the list of templates, as shown here:

    Figure
  3. Fill in the C++ Short Name on the Names page and select DHTML Control on the Options page, as shown here:

    Figure
    Figure
    NOTE
    If you look at the IDHTMLUI object, you’ll see this stock implementation of the OnClick handler:
    STDMETHOD(OnClick)(IDispatch* pdispBody, VARIANT varColor)
        {
            CComQIPtr<IHTMLBodyElement> spBody(pdispBody);
            if (spBody != NULL)
                spBody->put_bgColor(varColor);
            return S_OK;
        }

    The default OnClick handler uses QueryInterface on the IDispatch pointer to get the IHTMLBodyElement object. The handler then calls the put_bgColor method to change the background color.

  4. Compile, load, and run the control to see the ATL DHTML code in action.After you build the project, run the ActiveX Control Test Container from the Tools menu. In the test container, choose Insert New Control from the Edit menu and select CDHTML Object from the list box. Figure 29-5 shows the resulting ActiveX control that uses DHTML to change the background when the user clicks the button.

    Figure 29-5 The Ex29c ActiveX control.
    Figure 29-5. The Ex29c ActiveX control.