Click here to Skip to main content
15,880,405 members
Articles / Desktop Programming / MFC

HTMLCtrl with dynamic HTML content

Rate me:
Please Sign up or sign in to vote.
4.50/5 (22 votes)
27 Mar 2003CPOL5 min read 217.2K   3.2K   43   82
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)


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

www.robotecnik.com[^] - robots, CNC and PLC programming

Comments and Discussions

 
GeneralRe: Works on Win2k, crashes on XP Pin
Herbert Yu18-Mar-05 13:06
Herbert Yu18-Mar-05 13:06 
GeneralVS .NET 2003 Build Errors Pin
Brit17-Oct-03 9:47
Brit17-Oct-03 9:47 
GeneralRe: VS .NET 2003 Build Errors Pin
Joan M17-Oct-03 22:16
professionalJoan M17-Oct-03 22:16 
QuestionViewing html pages in CHTMLCtrl??? Pin
dom_beau7-Oct-03 11:30
dom_beau7-Oct-03 11:30 
AnswerRe: Viewing html pages in CHTMLCtrl??? Pin
Joan M7-Oct-03 19:55
professionalJoan M7-Oct-03 19:55 
GeneralRe: Viewing html pages in CHTMLCtrl??? Pin
dom_beau8-Oct-03 5:19
dom_beau8-Oct-03 5:19 
QuestionHow Installation in my Dialog Pin
TheReaper12083-Oct-03 4:51
TheReaper12083-Oct-03 4:51 
AnswerRe: How Installation in my Dialog Pin
Joan M3-Oct-03 4:56
professionalJoan M3-Oct-03 4:56 
Which kind of errors do you get, could you be more specific?

If I have more clues I'm sure I'll be able to help you...

I wait for your post...
GeneralRe: How Installation in my Dialog Pin
TheReaper12083-Oct-03 5:55
TheReaper12083-Oct-03 5:55 
GeneralRe: How Installation in my Dialog Pin
Joan M3-Oct-03 6:08
professionalJoan M3-Oct-03 6:08 
GeneralRe: How Installation in my Dialog Pin
TheReaper12083-Oct-03 6:11
TheReaper12083-Oct-03 6:11 
GeneralRe: How Installation in my Dialog Pin
Joan M3-Oct-03 6:15
professionalJoan M3-Oct-03 6:15 
GeneralRe: How Installation in my Dialog Pin
TheReaper12083-Oct-03 6:35
TheReaper12083-Oct-03 6:35 
GeneralCreate function Pin
Brit22-Jul-03 21:00
Brit22-Jul-03 21:00 
GeneralRe: Create function Pin
Joan M27-Jul-03 20:45
professionalJoan M27-Jul-03 20:45 
QuestionUsed in modeless dialog???? Pin
Frank Fesevur23-Jun-03 20:56
Frank Fesevur23-Jun-03 20:56 
AnswerRe: Used in modeless dialog???? Pin
Joan M17-Jul-03 5:16
professionalJoan M17-Jul-03 5:16 
AnswerRe: Used in modeless dialog???? Pin
Brit22-Jul-03 18:52
Brit22-Jul-03 18:52 
GeneralRe: Used in modeless dialog???? Pin
Frank Fesevur27-Jul-03 21:06
Frank Fesevur27-Jul-03 21:06 
Generalquestion about unicode Pin
sdent3-Apr-03 16:03
sdent3-Apr-03 16:03 
GeneralRe: question about unicode Pin
Joan M8-Apr-03 22:55
professionalJoan M8-Apr-03 22:55 
GeneralUsing CHtmlCtrl Pin
rrrado27-Mar-03 21:44
rrrado27-Mar-03 21:44 
GeneralRe: Using CHtmlCtrl Pin
Joan M27-Mar-03 22:10
professionalJoan M27-Mar-03 22:10 
GeneralRe: Using CHtmlCtrl Pin
rrrado28-Mar-03 1:07
rrrado28-Mar-03 1:07 
GeneralRe: Using CHtmlCtrl Pin
Joan M28-Mar-03 5:59
professionalJoan M28-Mar-03 5:59 

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.