Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

Integrating DHTML into MFC Views

Rate me:
Please Sign up or sign in to vote.
4.72/5 (23 votes)
30 Jan 20023 min read 397.7K   6.8K   106   94
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 <a href="http://msdn.microsoft.com/visualc/">Visual C++ .NET</a>  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.

Image 1

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 <mshelp:link tabindex="0" keywords="vcrefCDHtmlDialog"> 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. 

Image 2

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()
  • 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".

Image 3

 

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

Image 4

  • And name set your image as a resource type:

Image 5

  • 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


Written By
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

Comments and Discussions

 
Questionnothing works? Pin
gri19-Apr-03 13:57
gri19-Apr-03 13:57 
AnswerRe: nothing works? Pin
ChitMak23-Apr-03 15:14
ChitMak23-Apr-03 15:14 
GeneralUsing frames in CDHTMLView Pin
Antoon4-Apr-03 3:03
Antoon4-Apr-03 3:03 
GeneralWaiting for new version about these great work... Pin
KeithPRC12-Mar-03 19:15
KeithPRC12-Mar-03 19:15 
GeneralWhen i added CDhtmlDlg Class... Pin
WiseOgre7-Oct-02 20:37
WiseOgre7-Oct-02 20:37 
GeneralRe: When i added CDhtmlDlg Class... Pin
NormDroid27-Feb-03 20:37
professionalNormDroid27-Feb-03 20:37 
GeneralRe: When i added CDhtmlDlg Class... Pin
idealistt6-Mar-03 13:48
idealistt6-Mar-03 13:48 
GeneralRe: When i added CDhtmlDlg Class... Pin
MistER-j1-Oct-03 5:08
MistER-j1-Oct-03 5:08 
QuestionC#? Pin
Anonymous17-Aug-02 5:11
Anonymous17-Aug-02 5:11 
AnswerRe: C#? Pin
NormDroid9-Jan-03 22:22
professionalNormDroid9-Jan-03 22:22 
QuestionHow can I open a html file with CHtmlView Pin
Member 3264978-Aug-02 9:32
Member 3264978-Aug-02 9:32 
AnswerRe: How can I open a html file with CHtmlView Pin
Anonymous8-Aug-02 9:41
Anonymous8-Aug-02 9:41 
GeneralCSS Reference Pin
EddieBerman7-Aug-02 9:20
EddieBerman7-Aug-02 9:20 
GeneralRe: CSS Reference Pin
Lance Munslow8-Nov-02 4:23
sussLance Munslow8-Nov-02 4:23 
General? Change Resource Base Pin
Alec18-Jul-02 12:06
Alec18-Jul-02 12:06 
GeneralRe: ? Change Resource Base Pin
EddieBerman7-Aug-02 9:24
EddieBerman7-Aug-02 9:24 
General? OnBeginNavigate Pin
Synetech11-Jul-02 5:23
Synetech11-Jul-02 5:23 
GeneralRe: ? OnBeginNavigate Pin
Alec18-Jul-02 12:05
Alec18-Jul-02 12:05 
GeneralSomething is wrong .... Pin
Ryszard Krakowiak4-Jul-02 4:40
Ryszard Krakowiak4-Jul-02 4:40 
GeneralFix for Explorer style application Pin
Ryszard Krakowiak29-Jun-02 8:14
Ryszard Krakowiak29-Jun-02 8:14 
QuestionHow to send button click event to DHTMLView Pin
11-Jun-02 4:52
suss11-Jun-02 4:52 
AnswerRe: How to send button click event to DHTMLView Pin
cppguy10-Mar-03 17:41
cppguy10-Mar-03 17:41 
GeneralNewbie question... Pin
Miguel Lopes16-Apr-02 7:54
Miguel Lopes16-Apr-02 7:54 
GeneralRe: Newbie question... Pin
TBiker17-Apr-02 5:21
TBiker17-Apr-02 5:21 
GeneralRe: Newbie question... Pin
Miguel Lopes17-Apr-02 12:19
Miguel Lopes17-Apr-02 12:19 

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.