Click here to Skip to main content
13,862,864 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 20 Mar 2003
Licenced CPOL

HTMLCtrl with dynamic HTML content

, 27 Mar 2003
Rate this:
Please Sign up or sign in to vote.
How to dynamically change the contents of a HTML page inside a dialog and be able to handle HTML events.

Sample Image - HTMLCtrlSample.gif


This is my first article, and my English is not my best, so first of all I think you should excuse me for any inconvenience. Normally I write dialog based applications, and in my last project I needed to give the user an easier and more intuitive way to show him the required information. Then was when I thought on using HTML.

The first problem arose when I didn't want to use the WebBrowser ActiveX control and when I noticed that there wasn't any HTMLView that could be used in a dialog. Searching on the Internet I found the HTMLCtrl wrapper class from DiLascia and I began to use it. The only problem was that HTMLCtrl needed to load the web pages from disk, from Internet or from resources in the project... In all those cases the web page could not be modified without writing it to disk before showing it, and that was a big loss of time and processor. So then was when I searched the Internet again in order to find information on how to write directly to a web page without writing information to disk, and this article intends to explain the process to get that. I don’t want to say that my approach is the best one; even it could be that my approach would be the worst one, but it works, and if it can be the starting point for someone, I’ll be glad. (Notice that if somebody knows better ways to do this, I'll be very glad also to update this article).

Using the code

Previous notes

In the sample program, and in order to see how the HTML code gets changed, you just have to press the “&Modify” button.

In order to avoid getting some handle leaks and memory leaks that are generated when the HTMLCtrl is created and destroyed (internal leaks of HTMLView), you should create a hidden instance of HTMLCtrl in your main dialog as it's shown in the sample.

Microsoft has bugs in the CHTMLView class, so if you are using MFC6 and Microsoft VC++6 you are sensible to those bugs, there's a link regarding this in the Links and copyright issues section in the article.

Important functions in order to use the CHTMLCtrl Class:

  • CreateFromStatic(UINT nID, CWnd* pParent)

    This function is used in order to place the HTMLCtrl where you want and easily: you just need to place a static control where you want in the dialog editor and then in the OnInitDialog function, call this HTMLCtrl member function in order to change the static for the HTMLCtrl. This will make easier, the placing of the HTMLCtrl where you want it. The HTMLCtrl will get the ID of the static control. This function also creates a new document via the navigation to About:blank. More information regarding this in the Links section of the article.


  • ParseHTMLString(CString csStringToParse)

    This function is useful for people whose language has special HTML symbols, which can make the HTML content to be strange. I’m talking about accents and so on, also it can be used in order to modify the characters <, >and other special ones… This function automatically changes the incorrect characters for the right ones and this enables the user to avoid writing strange text in order to write those characters. Now it only changes the à á è é ì í ò ó ù ú for their respectives &grave/&cute…. This function can be called directly or indirectly via the second parameter of the next explained function. So I am not providing a calling sample (I prefer using the second parameter).

  • SetNewHTMLContent(CString csHTMLNewContent, bool bParse)

    This function is used in order to modify the content of the full HTML control by changing the member m_csContingutHTML; notice that this function will call automatically the SetHTMLEnDocument() function in order to modify the content in the browser, if the first document initialization is finished.


    • CString csHTMLNewContent: is the text string that conforms all the HTML code. Whatever you write here is what will be written in your HTML page. This is the code of the web page, you must include all the tags, refer to the sample in order to see the right formatting.
    • bool bParse: indicates if the HTMLCtrl will call the parser, (the previously explained function), by calling it you ensure that special characters are used as they should be.
  • OnAppCmd(LPCSTR lpszWhere)

    This function is used in order to get the HTML events and should be modified in each instance of the HTMLCtrl derived classes in the project. If you modify this function, you can redirect the pseudo app protocol to whatever you want (see notes on DiLascia's article).

Links and copyright issues

Known bugs

The bad news... OK this works well, you can modify the contents of your dialog based HTML browser without any effort, but there is one little bug... (I have not been able to find out how to solve it...) and I hope somebody can do it, so please, if someone finds how to solve it, I'll will try to update the article as soon as I can.

The problem is that creating the CHTMLCtrl and destroying it without a second instance of the same CHTMLCtrl, leaks memory and handles. So in order to avoid it, I have placed an invisible CHTMLCtrl instance inside the main dialog (always active).


  • 28/03/2003
    • Solved some bugs related to Microsoft.
    • Wait to document complete before trying to navigate to somewhere else.
    • General modification.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Joan M
Chief Technology Officer robotecnik
Spain Spain
I'm from Catalonia (in Spain) and a specialist in CNC, PLC, robotics and automation in general.

From 1998 I've been developing software applications for the automation market.

I'm using different technologies in each case to get the proper result, fieldbus communications, special electronics, special laser sensors, artificial vision, robot arms, CNC applications, PLC's...[^] - robots, CNC and PLC programming

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
Nagy Vilmos8-Apr-11 4:21
professionalNagy Vilmos8-Apr-11 4:21 
Generalhsow image Pin
JimmyO2-Dec-09 11:59
memberJimmyO2-Dec-09 11:59 
Generalevent change in webbrowser--help required Pin
santhi_malli27-Sep-07 4:52
membersanthi_malli27-Sep-07 4:52 
GeneralRe: event change in webbrowser--help required Pin
Joan M27-Sep-07 10:30
professionalJoan M27-Sep-07 10:30 
GeneralVS2005 CComPtr->Release() deprecated Pin
AlexXF26-Jun-07 0:03
memberAlexXF26-Jun-07 0:03 
GeneralFYI, getting an access violation in SetNewHTMLContent Pin
John Horigan15-Feb-07 20:41
memberJohn Horigan15-Feb-07 20:41 
GeneralURL navigation disappears to about:blank Pin
Kikokuhou1-Sep-06 13:07
memberKikokuhou1-Sep-06 13:07 
GeneralRe: URL navigation disappears to about:blank Pin
csc29-Sep-06 4:31
membercsc29-Sep-06 4:31 
Questionbugfix? Pin
Moehre4-Jan-06 3:26
memberMoehre4-Jan-06 3:26 
AnswerRe: bugfix? Pin
daviddddd19-Jun-07 4:51
memberdaviddddd19-Jun-07 4:51 
GeneralResizable dialog Pin
xSoptik25-Jul-05 2:55
memberxSoptik25-Jul-05 2:55 
GeneralDisplaying folder's content ... Pin
Marc Soleda6-May-05 7:58
memberMarc Soleda6-May-05 7:58 
GeneralAVI not displayed Pin
nicolad26-Apr-05 5:16
membernicolad26-Apr-05 5:16 
GeneralRe: AVI not displayed Pin
Joan M6-May-05 2:55
professionalJoan M6-May-05 2:55 
GeneralAVI not displayed Pin
nicolad26-Apr-05 5:16
membernicolad26-Apr-05 5:16 
GeneralI finally know why this crash when html size over, e.g. 60000 chars. Pin
Herbert Yu18-Mar-05 14:04
memberHerbert Yu18-Mar-05 14:04 
GeneralRe: I finally know why this crash when html size over, e.g. 60000 chars. Pin
Joan M6-May-05 2:59
professionalJoan M6-May-05 2:59 
GeneralCould you post your solution here? Pin
Herbert Yu10-Jun-05 10:34
memberHerbert Yu10-Jun-05 10:34 
QuestionHow to intercept the event from the WebBrowser control Pin
Hung2k27-Feb-05 19:47
memberHung2k27-Feb-05 19:47 
Generalsyntax error : 'ATL::CComPtr&lt;T&gt;' should be preceded by ':' Pin
Denis A. Vergara26-Feb-05 16:22
memberDenis A. Vergara26-Feb-05 16:22 
GeneralRe: syntax error : 'ATL::CComPtr&lt;T&gt;' should be preceded by ':' Pin
Joan M6-May-05 3:03
professionalJoan M6-May-05 3:03 
Generalwant IE and IpHelper Help Pin
eiteit23-Nov-04 6:10
membereiteit23-Nov-04 6:10 
GeneralRe: want IE and IpHelper Help Pin
Joan M23-Nov-04 6:34
professionalJoan M23-Nov-04 6:34 
GeneralRe: want IE and IpHelper Help Pin
eiteit23-Nov-04 21:31
membereiteit23-Nov-04 21:31 
Generalabout:blank Pin
JohnnyNin22-Apr-04 4:40
memberJohnnyNin22-Apr-04 4:40 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 28 Mar 2003
Article Copyright 2003 by Joan M
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid