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:
<span class="cpp-keyword">this</span>->m_cHTML.CreateFromStatic(IDC_STAT_HTML,<span class="cpp-keyword">this</span>);
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
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.