Click here to Skip to main content
13,351,050 members (56,318 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 29 Aug 2003

HtmlObj Template

, 29 Aug 2003
Rate this:
Please Sign up or sign in to vote.
Using a template class to handle IE object events


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.


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, 
  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)
      hr = S_OK;

      // TODO: add code to handle on mouse over events
      hr = S_OK;

      // TODO: add code to handle on mouse out events

  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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralLink error on VC7 using the demo above Pin
happyfisher20086-May-08 17:59
memberhappyfisher20086-May-08 17:59 
GeneralRe: Link error on VC7 using the demo above Pin
Wagner7-May-08 6:36
memberWagner7-May-08 6:36 
I have an idea on what is wrong but I am not sure what to do to fix this. It is not able to resolve CCmdTarget::interfaceMap when defining _GetBaseInterfaceMap. This is a necessary thing. It basically defines the next interface map that the command should be checked against.

All I can tell you is that this was originally written for VC6 not VC7, and I no longer work with VC and have not touched VC for 4 years now so I tried to see if there are others that have had the same problem and have an idea on how to fix this. Unfortunately, I have not found any. I probably would start from scratch with VC7 and currently I don’t have the time to investigate this beyond what I have already.

Good Luck
GeneralRe: Link error on VC7 using the demo above Pin
jehvmq1233-Dec-12 20:18
memberjehvmq1233-Dec-12 20:18 
GeneralRe: Link error on VC7 using the demo above Pin
Member 1034335211-Jan-14 2:16
memberMember 1034335211-Jan-14 2:16 
GeneralNot working with static link with MFC! Pin
internal23-Nov-04 22:40
memberinternal23-Nov-04 22:40 
Generallink error! Pin
yezw31-Aug-03 18:54
memberyezw31-Aug-03 18:54 
GeneralRe: link error! Pin
Wagner4-Sep-03 4:16
memberWagner4-Sep-03 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 30 Aug 2003
Article Copyright 2003 by Wagner
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid