Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C++

HtmlObj Template

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
29 Aug 20032 min read 57.5K   1.7K   27  
Using a template class to handle IE object events
// HtmlDocument2.cpp: implementation of the CHtmlDocument2 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "HtmlObjExample.h"
#include "HtmlDocument2.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHtmlDocument2::CHtmlDocument2()
	: CHtmlObj<IHTMLDocument2, &DIID_HTMLDocumentEvents>()
{
}

CHtmlDocument2::~CHtmlDocument2()
{
	while(m_aAnchors.GetSize())
	{
		CHtmlAnchorElement* pAnchorElem = m_aAnchors.GetAt(0);
		delete pAnchorElem;
		m_aAnchors.RemoveAt(0);
	}
}

// Shows how to create and adds a anchored element to the html document
int CHtmlDocument2::AddAnchor(CString strURL, CString strLabel)
{
	CComPtr<IHTMLElement> spBodyElem;
	if(FAILED(m_spHtmlObj->get_body(&spBodyElem)))
		return -1;

	// We first need to get the body dom node will be used for inserting the created
	// element.
	CComQIPtr<IHTMLDOMNode, &IID_IHTMLDOMNode> spHtmlBodyDomNode(spBodyElem);

	// Create an anchor element
	CComPtr<IHTMLElement> spElem;
	if(SUCCEEDED(m_spHtmlObj->createElement(CComBSTR("a"), &spElem)) && spElem)
	{
		CComQIPtr<IHTMLAnchorElement, &IID_IHTMLAnchorElement> spAnchorElem(spElem);
		if(spAnchorElem)
		{
			// Set it's particulars
			if(SUCCEEDED(spAnchorElem->put_href(CComBSTR(strURL))) &&	SUCCEEDED(spElem->put_innerText(CComBSTR(strLabel))))
			{
				// Get the anchors dom node and append it to the body dom node
				CComQIPtr<IHTMLDOMNode, &IID_IHTMLDOMNode> spHtmlAnchorDomNode(spAnchorElem);
				CComPtr<IHTMLDOMNode> spHtmlDomNode;
				if(SUCCEEDED(spHtmlBodyDomNode->appendChild(spHtmlAnchorDomNode, &spHtmlDomNode)))
				{
					CHtmlAnchorElement* pAnchorElement = new CHtmlAnchorElement(this);
					pAnchorElement->SetSite(spAnchorElem);

					return m_aAnchors.Add(pAnchorElement);
				}
			}
		}
	}

	return -1;
}

// Set the html message marquee
HRESULT CHtmlDocument2::SetMarquee(CComQIPtr<IHTMLMarqueeElement, &IID_IHTMLMarqueeElement>	spMessageMarquee)
{
	return m_htmlMessageMarquee.SetSite(spMessageMarquee);
}

// Allow access to set the text of the marquee easily through this method
HRESULT CHtmlDocument2::SetMarqueeMessage(CString strString)
{
	CComQIPtr<IHTMLElement, &IID_IHTMLElement> spElem(m_htmlMessageMarquee.m_spHtmlObj);
	if(!spElem)
		return E_FAIL;

	return spElem->put_innerText(CComBSTR(strString));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions