65.9K
CodeProject is changing. Read more.
Home

HtmlObj Template

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4 votes)

Aug 30, 2003

2 min read

viewsIcon

57882

downloadIcon

1683

Using a template class to handle IE object events

Introduction

This template class allows you to create Interface wrappers for any IHTML* (i.e. IHTMLElement, IHTMLAnchorElement, IHTMLImgElement, etc.) interface you want, and it includes code to handle the events of the connected browser objects.

Background

I was tired of always having to create class from the mshtml type library for various applications I was creating. I'm was always afraid that Microsoft was going to make changes to it and I would have to go back and recreate the class when ever this happens. So I was fooling around with wrapper class for the various browser objects. After I created these wrapper classes I thought it would be useful to get events from the objects. Especially for popup kill I was working on at the time. Once I had a pretty good idea on how this worked I started getting tiered of rewriting, or cutting and pasting in most situations, classes for every object that I wanted to control. So, I thought a template class would reduce development time. I am pretty happy with what I have so far and wanted to share it with you all.

Using the code

If you would like to use the template all you have to include is HtmlObj.h to your project. Once included in your project you then need to start making wrapper class derived from CHtmlObj<class T, const IID* piid>, where T is the interface that you want to wrap and *piid is the event interface you want to handle.

#include "HtmlObj.h"

class CHtmlAnchorElement : public CHtmlObj<IHTMLAnchorElement, 
        &DIID_HTMLAnchorEvents>  
{
public:
  CHtmlAnchorElement(CHtmlDocument2* pParentDoc2);
  virtual ~CHtmlAnchorElement();

  virtual HRESULT OnInvoke(DISPID dispidMember,REFIID riid, 
         LCID lcid, WORD wFlags,
      DISPPARAMS * pdispparams, VARIANT * pvarResult, 
         EXCEPINFO * pexcepinfo, 
         UINT * puArgErr);
};

Next you just need to override the OnInvoke mehtod.

HRESULT CHtmlAnchorElement::OnInvoke(DISPID dispidMember, 
      REFIID riid, LCID lcid, WORD wFlags,
      DISPPARAMS * pdispparams, VARIANT * pvarResult,
      EXCEPINFO * pexcepinfo, 
      UINT * puArgErr)
{
  HRESULT hr = E_NOTIMPL;
  switch(dispidMember)
  {
    case DISPID_HTMLELEMENTEVENTS_ONMOUSEOVER :
    {
      hr = S_OK;

      // TODO: add code to handle on mouse over events
      
      break;
    }
    case DISPID_HTMLELEMENTEVENTS_ONMOUSEOUT :
    {
      hr = S_OK;

      // TODO: add code to handle on mouse out events
      
      break;
    }
    default: 
    {
      break;
    }
  }

  return hr;
}

Now all you have to do is to create an instance of your new class and call SetSite() with the interface you want to handle.

Points of Interest

I tried to derive this template from both CCmdTarget and CComQIPtr but could not resolve the some deconstruction issues. If anyone is willing to give it a try I have some code complete.