Click here to Skip to main content
15,897,518 members
Articles / Desktop Programming / MFC

Using MSHTML Editing in VC6 Doc/View Applications

Rate me:
Please Sign up or sign in to vote.
4.82/5 (21 votes)
25 Apr 2004CPOL6 min read 128.9K   4.5K   69  
How to enable MSHTML editing in a view in VC6
// HTMLEditorView.cpp : implementation of the CHTMLEditorView class
//

#include "stdafx.h"
#include <Mshtml.h>
#include "HTMLEditor.h"

#include "HTMLEditorDoc.h"
#include "HTMLEditorView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHTMLEditorView

IMPLEMENT_DYNCREATE(CHTMLEditorView, CHtmlView)

BEGIN_MESSAGE_MAP(CHTMLEditorView, CHtmlView)
	//{{AFX_MSG_MAP(CHTMLEditorView)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CHtmlView::OnFilePrint)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHTMLEditorView construction/destruction

CHTMLEditorView::CHTMLEditorView()
{
	m_pHTMLDoc = (IHTMLDocument2 *) NULL;
}

CHTMLEditorView::~CHTMLEditorView()
{
	if (m_pHTMLDoc != (IHTMLDocument2 *) NULL)
		m_pHTMLDoc->Release();
}

BOOL CHTMLEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CHtmlView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHTMLEditorView drawing
void CHTMLEditorView::OnDraw(CDC* pDC)
{
	CHTMLEditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CHTMLEditorView::OnInitialUpdate()
{
	CHtmlView::OnInitialUpdate();

	// TODO: This code navigates to a popular spot on the web.
	//  change the code to go where you'd like.
	Navigate2(_T("http://www.codeproject.com/"), NULL, NULL);
}

/////////////////////////////////////////////////////////////////////////////
// CHTMLEditorView printing


/////////////////////////////////////////////////////////////////////////////
// CHTMLEditorView diagnostics

#ifdef _DEBUG
void CHTMLEditorView::AssertValid() const
{
	CHtmlView::AssertValid();
}

void CHTMLEditorView::Dump(CDumpContext& dc) const
{
	CHtmlView::Dump(dc);
}

CDocument* CHTMLEditorView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDocument)));
	return (CDocument*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHTMLEditorView message handlers
void CHTMLEditorView::OnNavigateComplete2(LPCTSTR strURL) 
{
	BSTR bs = SysAllocString(L"On");

	CHtmlView::OnNavigateComplete2(strURL);

	//	Get a pointer to our document
	m_pHTMLDoc = (IHTMLDocument2 *) GetHtmlDocument();
	

	if (m_pHTMLDoc != (IHTMLDocument2 *) NULL)
		m_pHTMLDoc->put_designMode(bs);

	SysFreeString(bs);
}

void CHTMLEditorView::Bold(BOOL bState)
{
	BSTR		 bCmd = SysAllocString(L"Bold");
	VARIANT_BOOL vb;
	VARIANT		 vValue;

	vValue.vt = VT_BOOL;
	vValue.boolVal = bState;

	if (m_pHTMLDoc != (IHTMLDocument2 *) NULL)
		m_pHTMLDoc->execCommand(bCmd, VARIANT_FALSE, vValue, &vb);

	SysFreeString(bCmd);
}

BOOL CHTMLEditorView::Bold()
{
	BSTR		 bCmd = SysAllocString(L"Bold");
	VARIANT_BOOL vb = FALSE;

	if (m_pHTMLDoc != (IHTMLDocument2 *) NULL)
		m_pHTMLDoc->queryCommandState(bCmd, &vb);

	SysFreeString(bCmd);
	return vb;
}

BOOL CHTMLEditorView::CanBold()
{
	BSTR		 bCmd = SysAllocString(L"Bold");
	VARIANT_BOOL vb = FALSE;

	if (m_pHTMLDoc != (IHTMLDocument2 *) NULL)
		m_pHTMLDoc->queryCommandEnabled(bCmd, &vb);

	SysFreeString(bCmd);
	return vb;
}

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