October 22, 2016
Hot Topics:

Building Dynamic Visual C++ Programs with DHTML

  • May 10, 1998
  • By V. Rama Krishna
  • Send Email »
  • More Articles »

The rich inteface provided by DHTML can be used in Visual C++ applications as a powerful alternative for Form Views. The Internet Explorer Document Object Model can be accessed from C++ programs using some of the interfaces the WebBrowser control exposes. The sample Test.exe illustrates this.

The sample code illustrates follwing :-

1. Hosting the web browser control

2. Replacing the default context menu of IE4 with our own.

3. Accessing the IE4 document object model

4. Running a piece of C++ code in response to the events fired from the HTML document.

There are various steps in building a program with DHTML.

1. It is important that the latest WIN32 lib files and Include files must be presented. They can be freely downloaded as a part of internet client SDK form www.microsoft.com/msdn/sdks/inetsdk.

2. The next step is to host the web browser control. The view class has been used for this purpose. A class has been derived from CView - CBrowserView for this purpose.

//Create the Browser Control
if (!m_wndWebBrowser.CreateControl(CLSID_WebBrowser, NULL,WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP, CRect(0,0,0,0), this, 1))
return -1;

//Get IUnknown Interface pointer
LPUNKNOWN pUnk = m_wndWebBrowser.GetControlUnknown();
HRESULT hr = pUnk->QueryInterface(IID_IWebBrowser2, (LPVOID*)&m_pWebBrowser2);

if (!SUCCEEDED(hr))
return -1;

The IWebBrowser2 interface needs to be obtained for various functions such as Navigate etc.

3. A special class CHTMLForm has been used. Since a single web browser control can load many different HTML filesit is better to encapsulate a URL into a class just like CFormView encapsulates a dialog resource. To load a form CBrowserView::LoadForm can be used.




if (!SUCCEEDED(hr))
return FALSE;

return TRUE;

4. The next step is to access the HTML form elements from C++ code. They can be accessed once the Document has been fully loaded and DocumentComplete event has been fired.

void CBrowserView::OnDocumentComplete(LPDISPATCH pDisp,


//Document is fully loaded now initialize any variables
CWaitCursor wc;

if (!m_bNewForm)

IHTMLDocument2* pHTMLDocument = NULL;
IHTMLElementCollection* pCollection = NULL;

hr = m_pWebBrowser2->get_Document(&pDisp1);
//if (!SUCCEEDED(hr))
// throw Exception

hr = pDisp1->QueryInterface(IID_IHTMLDocument2, (void**)&pHTMLDocument);
//if (!SUCCEEDED(hr))
// throw Exception

hr = pHTMLDocument->get_all(&pCollection);
//if (!SUCCEEDED(hr))
// throw Exception
ICustomDoc* pCustomDoc;

hr = pDisp1->QueryInterface(IID_ICustomDoc, (LPVOID*)&pCustomDoc);
//if (!SUCCEEDED(hr))
// throw Exception

CDocHostUIHandler* pDHUIH = new CDocHostUIHandler();

pDHUIH->m_pBrowserView = this;



//It is responsiblity of the form to realease pCollection


m_bNewForm = FALSE;


There are many interfaces through which the Document Object Model can be accessed most important of this is IHTMLCollection. For details refer to Internet Client SDK. Next, the default IE4 context menu can be replaced by setting it's IDocHostUIHandler through ICustomDoc::SetUIHandler as shown in the code.

6. The individual HTML elements can be accessed using GetElement or GetInputElement function in CHTMLForm.

7. Once the individual elements are obtained Sky is the limit. The HTML can be modified in whatever wa one likes.

8. The example also illustrates how to handle eevnts from within the HTML documents like clicking of a button or a hyperlink to run a piece of C++ code.

This has been done using "cmd://.." as URL prefix. When the browser starts to Navigate this URL it fires BeforeNavigateEvent. In the event handler the URL is parsed to see if it is a command and then the application can handle command in it's own way.

The sample has 2 projects :-

1. BrowseView.dsp - Builds an extension dll mfcext.dll which contains the classes CBrowserView, CHTMLForm and CDocHostUIHandler.

2. Test.dsp a project that builds an SDI application using the extension dll. This uses the student registration database that comes with VC++ 5.0.

To use the sample :-

1. Install latest Win32 API from Internet Client SDK.

2. Register the Student Registration Database that comes with VC++ 5.0.

3. Use the HTML file Inst.htm to be used as a form for Instructor Data Entry.

Download demo project - 144KB

Download source - 139KB

Date Posted: 10 May 1998

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel