Click here to Skip to main content
15,886,736 members
Articles / Desktop Programming / MFC

Using IHTMLEditDesigner

Rate me:
Please Sign up or sign in to vote.
4.88/5 (40 votes)
24 Apr 2004CPOL9 min read 182.5K   3.5K   74  
Using IHTMLEditDesigner to modify IE's editing behaviour
#include "StdAfx.h"
#include "variant.h"
#include <Mshtmdid.h>

#include ".\Chtmleditdesigner.h"

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
{
	HRESULT hrRet = S_OK;

	// Standard QI() initialization - set *ppv to NULL.
	*ppvObject = NULL;

	// If the client is requesting an interface we support, set *ppvObject.
	if (IsEqualIID(riid, IID_IUnknown))
		*ppvObject = (IUnknown *) this;
	else if (IsEqualIID(riid, IID_IHTMLEditDesigner))
		*ppvObject = (IHTMLEditDesigner *) this;
	else
		// We don't support the interface the client is asking for.
		hrRet = E_NOINTERFACE;

	// If we're returning an interface pointer, AddRef() it.
	if (S_OK == hrRet)
		((IUnknown *) *ppvObject)->AddRef();

	return hrRet;
}

ULONG STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::AddRef(void)
{
	return ++m_uRefCount;
}

ULONG STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::Release(void)
{
	return --m_uRefCount;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::PreHandleEvent(DISPID inEvtDispId, IHTMLEventObj *pIEventObj)
{
	USES_CONVERSION;
	IHTMLElement  *pSel;
	BSTR		  b = BSTR(NULL);

	if (inEvtDispId == DISPID_HTMLELEMENTEVENTS2_ONMOUSEDOWN)
	{
		if (pIEventObj != (IHTMLEventObj *) NULL)
		{
			pIEventObj->get_srcElement(&pSel);

			//	We've got our source element, get it's inner text
			if (pSel != (IHTMLElement *) NULL)
			{
				pSel->get_tagName(&b);

				if (_tcsicmp("IMG", W2A(b)) == 0)
				{
					//	We only install the ondrag handler if the
					//	tag is an image.
					VARIANT v;

					v.vt = VT_DISPATCH;
					v.pdispVal = &m_dp;

					pSel->put_ondragstart(v);
					pSel->get_innerText(&b);
				}
			}
		}
	}

	return S_FALSE;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::PostHandleEvent(DISPID /*inEvtDispId*/, IHTMLEventObj * /*pIEventObj*/)
{
	return S_FALSE;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::TranslateAccelerator(DISPID /*inEvtDispId*/, IHTMLEventObj * /*pIEventObj*/)
{
	return S_FALSE;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragHTMLEditDesigner::PostEditorEventNotify(DISPID /*inEvtDispId*/, IHTMLEventObj * /*pIEventObj*/)
{
	return S_FALSE;
}

CMSHTMLDisableDragHTMLEditDesigner::CMSHTMLDisableDragHTMLEditDesigner()
{
	m_pServices = (IHTMLEditServices *) NULL;
	m_uRefCount = 0;
}

BOOL CMSHTMLDisableDragHTMLEditDesigner::Attach(IHTMLDocument2 *pDoc)
{
	if (m_pServices	!= (IHTMLEditServices *) NULL)
		m_pServices->Release();

	IServiceProvider *pTemp;

	if (pDoc ==	(IHTMLDocument2	*) NULL)
		return FALSE;

	pDoc->QueryInterface(IID_IServiceProvider, (void **) &pTemp);

	if (pTemp != (IServiceProvider *) NULL)
	{
		pTemp->QueryService(SID_SHTMLEditServices, IID_IHTMLEditServices, (void	**)	&m_pServices);

		if (m_pServices	!= (IHTMLEditServices *) NULL)
		{
			m_pServices->AddDesigner(this);
			return TRUE;
		}
	}

	return FALSE;
}

void CMSHTMLDisableDragHTMLEditDesigner::Detach()
{
	if (m_pServices != (IHTMLEditServices *) NULL)
		m_pServices->RemoveDesigner(this);
}

//	CResiEditDispatch
HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
{
	HRESULT hrRet = S_OK;

	// Standard QI() initialization - set *ppv to NULL.
	*ppvObject = NULL;

	// If the client is requesting an interface we support, set *ppv.
	if (IsEqualIID(riid, IID_IUnknown))
		*ppvObject = (IUnknown *) this;
	else if (IsEqualIID(riid, IID_IDispatch))
		*ppvObject = (IDispatch *) this;
	else
		// We don't support the interface the client is asking for.
		hrRet = E_NOINTERFACE;

	// If we're returning an interface pointer, AddRef() it.
	if (S_OK == hrRet)
		((IUnknown *) *ppvObject)->AddRef();

	return hrRet;
}

ULONG STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::AddRef(void)
{
	return ++m_uRefCount;
}

ULONG STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::Release(void)
{
	return --m_uRefCount;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::GetTypeInfoCount(UINT *pctinfo)
{
	*pctinfo = 0;
	return S_OK;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::GetTypeInfo(UINT /*iTInfo*/, LCID /*lcid*/, ITypeInfo ** /*ppTInfo*/)
{
	return DISP_E_BADINDEX;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::GetIDsOfNames(REFIID /*riid*/, LPOLESTR * /*rgszNames*/, UINT /*cNames*/, LCID /*lcid*/, DISPID * /*rgDispId*/)
{
	return DISP_E_UNKNOWNNAME;
}

HRESULT STDMETHODCALLTYPE CMSHTMLDisableDragDispatch::Invoke(DISPID /*dispIdMember*/, REFIID /*riid*/, LCID /*lcid*/, WORD /*wFlags*/, DISPPARAMS * /*pDispParams*/, VARIANT *pVarResult, EXCEPINFO * /*pExcepInfo*/, UINT * /*puArgErr*/)
{
	//	If we were installed it means we should disable
	//	dragging. So set the return value to false
	pVarResult->vt = VT_BOOL;
	pVarResult->boolVal = false;
	return S_FALSE;
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been programming for 35 years - started in machine language on the National Semiconductor SC/MP chip, moved via the 8080 to the Z80 - graduated through HP Rocky Mountain Basic and HPL - then to C and C++ and now C#.

I used (30 or so years ago when I worked for Hewlett Packard) to repair HP Oscilloscopes and Spectrum Analysers - for a while there I was the one repairing DC to daylight SpecAns in the Asia Pacific area.

Afterward I was the fourth team member added to the Australia Post EPOS project at Unisys Australia. We grew to become an A$400 million project. I wrote a few device drivers for the project under Microsoft OS/2 v 1.3 - did hardware qualification and was part of the rollout team dealing directly with the customer.

Born and bred in Melbourne Australia, now living in Scottsdale Arizona USA, became a US Citizen on September 29th, 2006.

I work for a medical insurance broker, learning how to create ASP.NET websites in VB.Net and C#. It's all good.

Oh, I'm also a Kentucky Colonel. http://www.kycolonels.org

Comments and Discussions