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

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

Introduction

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.

    Sample:

    this->m_cHTML.CreateFromStatic(IDC_STAT_HTML,this);
  • 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.

    Parameters

    • 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.
      this->m_cHTML.SetNewHTMLContent(csHTML,true);
  • 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).

History

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

License

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

About the Author

Joan Murt
Chief Technology Officer
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 special machines we make.
 
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...
 
You can see some of my work here: www.tamautomation.com
 
And some videos in our YouTube channel

Comments and Discussions

 
GeneralMy vote of 4 PinmemberNagy Vilmos8-Apr-11 3:21 
Generalhsow image PinmemberJimmyO2-Dec-09 10:59 
Generalevent change in webbrowser--help required Pinmembersanthi_malli27-Sep-07 3:52 
GeneralRe: event change in webbrowser--help required PinmemberJoan Murt27-Sep-07 9:30 
GeneralVS2005 CComPtr->Release() deprecated PinmemberAlexXF25-Jun-07 23:03 
GeneralFYI, getting an access violation in SetNewHTMLContent PinmemberJohn Horigan15-Feb-07 19:41 
GeneralURL navigation disappears to about:blank PinmemberKikokuhou1-Sep-06 12:07 
GeneralRe: URL navigation disappears to about:blank Pinmembercsc29-Sep-06 3:31 
Questionbugfix? PinmemberMoehre4-Jan-06 2:26 
AnswerRe: bugfix? Pinmemberdaviddddd19-Jun-07 3:51 
GeneralResizable dialog PinmemberxSoptik25-Jul-05 1:55 
GeneralDisplaying folder's content ... Pinmembermarcdev6-May-05 6:58 
GeneralAVI not displayed Pinmembernicolad26-Apr-05 4:16 
GeneralRe: AVI not displayed PinmemberJoan Murt6-May-05 1:55 
GeneralAVI not displayed Pinmembernicolad26-Apr-05 4:16 
GeneralI finally know why this crash when html size over, e.g. 60000 chars. PinsussHerbert Yu18-Mar-05 13:04 
GeneralRe: I finally know why this crash when html size over, e.g. 60000 chars. PinmemberJoan Murt6-May-05 1:59 
GeneralCould you post your solution here? Pinmemberyuhaian10-Jun-05 9:34 
QuestionHow to intercept the event from the WebBrowser control PinmemberHung2k27-Feb-05 18:47 
Generalsyntax error : 'ATL::CComPtr&lt;T&gt;' should be preceded by ':' Pinmemberdemisvergara26-Feb-05 15:22 
GeneralRe: syntax error : 'ATL::CComPtr&lt;T&gt;' should be preceded by ':' PinmemberJoan Murt6-May-05 2:03 
Generalwant IE and IpHelper Help Pinmembereiteit23-Nov-04 5:10 
GeneralRe: want IE and IpHelper Help PinmemberJoan Murt23-Nov-04 5:34 
GeneralRe: want IE and IpHelper Help Pinmembereiteit23-Nov-04 20:31 
Generalabout:blank PinmemberJohnnyNin22-Apr-04 3:40 

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Mar 2003
Article Copyright 2003 by Joan Murt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid