Click here to Skip to main content
Click here to Skip to main content

Integrating DHTML into MFC Views

, 30 Jan 2002
Rate this:
Please Sign up or sign in to vote.
Demonstrates integrating DHTML into MFC Views, and using HTML in views to get feedback from the browser via connection points

Prerequisites: This article is targeted for Visual C++ .NET  and the author assumes the reader has a good understanding of C++, MFC, OLE, HTML. 

What this article does: Uses  HTML in views to get feed back from the browser via connection points. It does not open or save documents, that's for the developer to implement.

Motive

A while back I had to develop a design tool to edit form based style pages. After completing the project, Visual Studio .NET came into BETA, my initial thoughts after being presented with the "Start Page" was WOW, I wanted this feature in my application, after all products like Outlook and InstallShield had been doing this kind of UI for years (slight exaggeration) . Previously I had spent a while writing a wrapper class that implemented sinks from the IE browser control so I could get feed back from actions such as mouse click, button click etc. The problem with the class I designed was that the class was rigid and could only be used to implement start pages. Since the release of Visual C++ 7.0 I discovered  a new class which allow DHTML interaction with MFC. This class is called CDHTMLDialog, this class had implemented a generic framework to sinks IE Events back into MFC, but this class was limited to dialog boxes and I was surprised that Microsoft hadn't provided a CView based version. What I needed was a CDHTMLView class. With that in mind I developed a class to do just that!

Design

After poking around the source code that shipped with Visual C++ .NET I found the  code that implemented IHTMLElement sinks. I then had the basis of designing the view. Here the class diagram. 

As you can see from the class schema, CDHTMLView inherits from CView and CDHTMLEventSink.

CDHTMLEventSink is a new class that ships with Visual C++ .NET, 

Implementation

  • Decide which view is going to be your DHTML View
  • Include DHTMLView.h & DHTMLView.cpp in your project
  • Edit your Views header file and add the follow code snippets in BOLD
#include "DHTMLView.h"
class CStartPrjView : public CDHTMLView
{
	 	 
// Class body...
protected:
	DECLARE_MESSAGE_MAP()
	DECLARE_DHTML_EVENT_MAP()
public:
	virtual void OnInitialUpdate();
}
  •    Now add the event map to your for view's .cpp file
BEGIN_DHTML_EVENT_MAP(CStartPrjView)
END_DHTML_EVENT_MAP()
<!------------------------------- That's it! --------------------------->
  • Once the class has been declared its time to insert some functionality. The first thing we need to do now is to insert some Html into the view, this can be done design your HTML and using the resource editor import a HTML file.
  • Any images must be imported as resource type "2110" and name your images as strings e.g. "StartGif".

 

  • Open the HTML in the Resource Design mode and select your image.

  • And name set your image as a resource type:

  • Now we need to tie up HTML element with events, this is done by editing the BEGIN_DHTML_EVENT_MAP and inserting message handlers as you would with normal MFC message handlers. One thing to note here is the IDs I gave each element in my document an ID. This allows me to map to the create event.
BEGIN_DHTML_EVENT_MAP(CStartPrjView)
	DHTML_EVENT_ONCLICK(_T("MRU1"), OnClickMRU1)	
	DHTML_EVENT_ONCLICK(_T("MRU2"), OnClickMRU1)
	DHTML_EVENT_ONCLICK(_T("MRU3"), OnClickMRU1)
	DHTML_EVENT_ONCLICK(_T("MRU4"), OnClickMRU1)
	 
	DHTML_EVENT_ONCLICK(_T("NewFile"), OnNewFile)
	DHTML_EVENT_ONCLICK(_T("OpenFile"), OnOpenFile)
END_DHTML_EVENT_MAP()
  • Now we have to wire the handlers in the header and implementation file
	 	
	HRESULT OnClickMRU1(IHTMLElement *phtmlElement);
	HRESULT OnClickMRU1(IHTMLElement *phtmlElement);
	HRESULT OnClickMRU1(IHTMLElement *phtmlElement);
	HRESULT OnClickMRU1(IHTMLElement *phtmlElement);
	HRESULT OnNewFile(IHTMLElement *phtmlElement);
	HRESULT OnOpenFile(IHTMLElement *phtmlElement);
  • and implementation file
HRESULT CStartPrjView::OnClickMRU1(IHTMLElement *phtmlElement)
{
	CComBSTR bstr;
	phtmlElement->get_innerText(&bstr);
	if (bstr)
	{
		GetDocument()->SetTitle(CString(bstr) );
		CString s;
		s.Format("File %s selected", CString(bstr));
		AfxMessageBox(s, MB_ICONINFORMATION);
 
	}
	return S_FALSE;
}
HRESULT CStartPrjView::OnClickMRU2(IHTMLElement *phtmlElement)
{
	CComBSTR bstr;
	phtmlElement->get_innerText(&bstr);
	if (bstr)
	{
		GetDocument()->SetTitle(CString(bstr) );
		CString s;
		s.Format("File %s selected", CString(bstr));
	AfxMessageBox(s, MB_ICONINFORMATION);
	}
	return S_FALSE;
}
HRESULT CStartPrjView::OnClickMRU3(IHTMLElement *phtmlElement)
{
	CComBSTR bstr;
	phtmlElement->get_innerText(&bstr);
	if (bstr)
	{
		GetDocument()->SetTitle(CString(bstr) );
		CString s;
		s.Format("File %s selected", CString(bstr));
		AfxMessageBox(s, MB_ICONINFORMATION);
	}
	return S_FALSE;
}
HRESULT CStartPrjView::OnClickMRU4(IHTMLElement *phtmlElement)
{
	CComBSTR bstr;
	phtmlElement->get_innerText(&bstr);
	if (bstr)
	{
		GetDocument()->SetTitle(CString(bstr) );
		CString s;
		s.Format("File %s selected", CString(bstr));
		AfxMessageBox(s, MB_ICONINFORMATION);
	}
	return S_FALSE;
}
 
HRESULT CStartPrjView::OnNewFile(IHTMLElement *phtmlElement)
{
	AfxMessageBox("New file Pressed" , MB_ICONINFORMATION);
	return S_FALSE;
}
HRESULT CStartPrjView::OnOpenFile(IHTMLElement *phtmlElement)
{
	AfxMessageBox("Open file Pressed" , MB_ICONINFORMATION);
	return S_FALSE;
}

Conclusion

The CDHTMLView class provides a stable framework for hooking HTML elements into your code, one thing I should add there is a whole raft of data exchange macros to exchange data with MFC and HTML eg. DDX_DHtml_ElementInnerText. This macros are all defined in the DHTMLView.h file.

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

Share

About the Author

NormDroid
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom



Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.

We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net
 
Visit Software Kinetics
Follow on   Twitter

Comments and Discussions

 
QuestionWhich license is applied to "DHTMLView_src"? PinmemberTakeoji31212-Jun-13 17:34 
QuestionRefresh Problem Pinmemberpqr12348-Jul-11 22:35 
Questionhow can I integrate it in a vc6.0 IDE? Pinmemberori kovacsi17-Dec-07 23:27 
GeneralNot working with IE 7 Beta 3 PinmemberBuzz18-Oct-06 19:24 
GeneralRe: Not working with IE 7 Beta 3 Pinmembernorm .net18-Oct-06 21:48 
Maybe, I should dust off my MFC overalls today, and do some goold old fashion MFC, I take a look and if need my add a revised project or fix.

 
We made the buttons on the screen look so good you'll want to lick them. Steve Jobs

GeneralRe: Not working with IE 7 Beta 3 PinmemberBuzz18-Oct-06 22:11 
QuestionHow to Place ActiveX control on HTMLview PinmemberAbhishek Narula13-Sep-06 1:29 
GeneralIE7 and DHTMLView PinmemberJMBezeau2-May-06 4:57 
Generaldoes not make dll or static library PinmemberYoungho Kim1-Apr-06 19:32 
GeneralDHTMLView with Framesets PinmemberYomok9-Mar-06 20:36 
QuestionHow to iterate over all html elements PinmemberDan Baker9-Mar-06 9:05 
AnswerRe: How to iterate over all html elements PinmemberJMBezeau2-May-06 5:05 
QuestionUpdating tag attributes Pinmembergeregam2-Jan-06 9:25 
Generalupdate of views Pinmemberrajukvrk7819-Oct-05 5:47 
GeneralCatch windows messages or special keys. Pinmemberarkebuzy10-Jul-05 21:29 
GeneralRe: Catch windows messages or special keys. PinmemberNimstad29-Aug-05 0:28 
GeneralShowContextMenu() PinmemberAbhishek Narula6-Jul-05 23:52 
GeneralRe: ShowContextMenu() Pinmembermcbain3-Nov-05 18:21 
GeneralRe: ShowContextMenu() Pinmemberluowei8816-Dec-07 0:18 
GeneralGreat Work PinsussJonathan Schmidt27-Nov-04 14:29 
Generalflickering and redraw PinsussDaniele Piazza4-Aug-04 6:05 
GeneralRe: flickering and redraw PinmemberNimstad9-Aug-04 0:25 
GeneralRe: flickering and redraw Pinmembernorm.net9-Aug-04 4:34 
GeneralRe: flickering and redraw PinmemberUTCNathan18-Oct-05 16:25 
QuestionWhy a View!? PinmemberLoz27-Jan-04 9:44 
AnswerRe: Why a View!? PinmemberNimstad31-Jan-04 12:05 
GeneralRe: Why a View!? PinmemberLoz3-Feb-04 10:47 
GeneralLoading DHTML from memory Pinsussmpasqual14-Dec-03 13:00 
GeneralRe: Loading DHTML from memory PinmemberNimstad15-Dec-03 4:49 
GeneralRe: Loading DHTML from memory Pinsussmpasqual18-Dec-03 22:22 
GeneralRe: Loading DHTML from memory PinmemberNimstad18-Dec-03 23:01 
GeneralRe: Loading DHTML from memory PinmemberLoz27-Jan-04 11:03 
GeneralShowContextMenu PinmemberMistER-j20-Oct-03 5:54 
GeneralRe: ShowContextMenu PinmemberWolfSupernova28-Oct-03 8:28 
GeneralRe: ShowContextMenu PinmemberWolfSupernova28-Oct-03 14:32 
GeneralRe: ShowContextMenu PinmemberMistER-j28-Oct-03 23:53 
GeneralGetting element text question PinmemberMatt Gates8-Sep-03 8:15 
GeneralRe: Getting element text question Pinmemberidealistt24-Sep-03 17:21 
GeneralRe: Getting element text question PinmemberMatt Gates25-Sep-03 9:31 
QuestionAnyone used other controls? Pinmembermiggedy1-Aug-03 8:48 
AnswerRe: Anyone used other controls? PinsussAnonymous24-Sep-03 17:12 
GeneralProblem with LoadFromResource() and GetElement() Pinmembermschlestein19-Jun-03 7:11 
GeneralRe: Problem with LoadFromResource() and GetElement() PinmemberThomas Nimstad30-Jul-03 23:12 
GeneralDHTMLView: XML data used in HTML document Pinmemberwk@ld27-May-03 10:06 
QuestionPrinting fails? PinmemberFloSoft22-May-03 22:54 
AnswerRe: Printing fails? PinmemberFloSoft22-May-03 23:09 
AnswerRe: Printing fails? PinmemberJiang Sheng30-Jun-03 18:37 
Questionabout onkeydown event? Pinmemberidealistt15-May-03 0:19 
AnswerRe: about onkeydown event? PinsussThomas Nimstad23-Jul-03 7:17 
GeneralRe: about onkeydown event? Pinmemberidealistt24-Sep-03 22:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 31 Jan 2002
Article Copyright 2002 by NormDroid
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid