Loading Simple HTML Pages From Memory
Sometimes,when you use web browser control, it's not suitable to
work with temporary files as a source for browser navigation. For example, if you
want secure information or you need dynamically change content from c++
program.
CMoreHtml class derived from standard MFC-class
CHtmlView and fully keeps class functionality. Additionally you will have some
more abilities to work with html content. This class use standard COM
interfaces IWebBrowser2, IHtmlDocument2. Moreover in this application uses
IMarkupServices interface it force to minimum capability with IE 5.0.
Class usage
You could derive your own view from CMoreHtmlView like standard CHtmlView. If you want to activate work with memory, you need just call NavigateMemory() function like showed in example:
void CYourOwnView::OnInitialUpdate()
{
CHtmlView::OnInitialUpdate();
// TODO: This code navigates to a popular spot on the web.
// change the code to go where you'd like.
//Navigate2(_T("http://www.microsoft.com/visualc/"),NULL,NULL);
//initialize navigation from memory mode
NavigateMemory();
//set parameters of html
SetTitle( "Shakespeare page");
PutBodyContent( (LPSTR)lpcstrContent);
COleVariant ovBgColor( "cornsilk");
SetBodyAttributes( "BGCOLOR", ovBgColor);
SetScript( (LPSTR)lpcstrScript);
}
In any case, you need source for correct working of web browser control. Thereto, you can use html-file template from resource.
<HTML> <HEAD> <TITLE></TITLE> <SCRIPT>>/SCRIPT> </HEAD> <BODY> </BODY> </HTML>
I can't find possibility to write to <HTML> or <HEAD> tag content, therefore I've created several functions, they write to exact parts of html document.
| BOOL PutBodyContent( LPSTR lpstrContent) | Write html content ( filling tag). lpstrContent - text of HTML content; |
| BOOL SetBodyAttributes(CString sName, ColeVariant vValue) | Setting tag attributes, like BGCOLOR, LINK etc. sNname - attribute name; vValue - attribute value(VARIANT type); |
| BOOL SetCharset(LPSTR lpstrCharset) | Setting Charset. |
| BOOL SetScript(LPSTR lpstrScript) | Write html scripts. lpstrScript - content of <SCRIPT> tag; |
| BOOL SetTitle(LPSTR lpstrTitle) | Set title of html document; |
Uploading of html content possible only when downloading of html template finished. But you could use these functions in any place of code after running NavigateMemory(). Values of content store in CMoreHtmlView class members and apply in exact moment.
How CMoreHtmlView works.
NavigateMemory() function loads template html document from resource
with identifier IDR_EMPTY_HTML and sets m_bMemoryMode to TRUE.
PutBodyContent, SetScript etc. functions call IHtmlDocument2 methods
only if document object exists, in other case functions only store
content in private variables. If these functions execute with NULL
parameter then content modification does from member variable values.
OnDownloadComplete function overloading makes for reloading html content
if refresh browser command will be called.
Search and mark words in html document
Sometime you need make your own search system in application that uses web browser control. MarkWords member function you could use in that case. You only need to pass a search string as input parameter.
for( ; ;)
{
// Find text
hr = pPtr1->FindText( poSearchStr, 0, pPtr2, NULL );
if (hr == S_FALSE) // did not find the text
break;
IHTMLElement *pFontEl;
// create FONT element with attributes for selection
OLECHAR pchAttributes=0;
hr=pMS->CreateElement(TAGID_FONT,
L"COLOR=white STYLE=\"backgroundcolor:darkblue\"", &pFontEl);
// Insert created element to context
hr=pMS->InsertElement( pFontEl, pPtr1, pPtr2);
// Continue searching
pPtr1->MoveToPointer( pPtr2);
}
It's main part of MarkWords function, at the beginning makes search of
string after that creates <FONT COLOR=white
STYLE="backgroundcolor:darkblue">...</FONT> element.
This tag marks the located string.

Comments
Load xml with xslt from memory
Posted by alexiworld on 05/14/2004 10:33amI have in memory the content of xml file which refers to xslt file on disk. I am trying to force the browser to display this content. But it doesn't work as I expected! :(( Otherwise if I save this content as temporary file on disk and make browser to navigate to new location everything is fine. So can you answer me what to do to make things workings without making temporary file?! Best regards, Alex
Replyimplemented memory table in a dll causes acess violation
Posted by Legacy on 08/27/2003 12:00amOriginally posted by: John
Hello,
I have taken the sample for a memory html into a dll, where the first call to it succeeds, but a second navigation to the resource from a new instance of the class causes an access violation in urlmon.dll when navigate2 is called.
Anyone else experience a similar problem ?
Is the resource getting locked ?
Thanks,
ReplyJohn
This seemed not work correct with Unicode text
Posted by Legacy on 08/19/2003 12:00amOriginally posted by: Hung N
I used this to load a Unicode text, then I don't understand why all the links with <A HREF... are not activated, the text seemed to be displayed as pur plain text in black, with only bigger size for <H?> tags but no color or links
Any idea ?
ReplyI want to process HTML form with C Program.
Posted by Legacy on 04/08/2003 12:00amOriginally posted by: Bhavesh Patel
Hi Sir,
I want a program that can process HTML form. CGI Sort of programming.
With Regards..
ReplyBhavesh
Encoding issues
Posted by Legacy on 03/24/2003 12:00amOriginally posted by: oren
When i use the component (as a part of MDI app) and try right clicking on it and change the encoding nothing happends (ie the page reloads so i get another download complete event, but the encoding doesn't change).
any ideas ?
Oren
ReplyNeed hint. How to programmatically swap contents of two WebBrowsers.
Posted by Legacy on 02/06/2003 12:00amOriginally posted by: Oleg Schildt
ReplyA simpler way
Posted by Legacy on 10/01/2002 12:00amOriginally posted by: Nom De Plume
ReplyHere is how to add < STYLE > to this!
Posted by Legacy on 09/30/2002 12:00amOriginally posted by: Sander Verhagen
ReplyHow to put "FrameSet" tag ?
Posted by Legacy on 09/06/2002 12:00amOriginally posted by: SA
I put "<frameset>" tag to MSHTML.
I tried,,,
1.put_body
2.IHTMLDocument2::write
But "FrameSet" tag didn't insert.
#1. "FrameSet" Tag wasn't inserted.
#2. "FrameSet" Tag was inserted,But Page is blank.
I think hardly.I ask to many people.
But The problem does resolve.
Please tell me & help !!!!
Replycool stuff but still show exception warning... anyone has a clue
Posted by Legacy on 07/12/2002 12:00amOriginally posted by: Eric
Thanks for sharing. This is what I'm looking for.
Anyway, when I run in debug mode, I see a warning like this.
Warning: constructing COleException, scode = DISP_E_MEMBERNOTFOUND ($80020003).
First-chance exception in MoreHtmlViewDemo.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.
Anyone has a clue why and is there a way to fix it.
Thanks in advance.
Eric
ReplyLoading, Please Wait ...