Loading Simple HTML Pages From Memory

Environment: VC6 SP4, IE 5.0, Platform SDK.

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.

Downloads

Download demo project and source – 29 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read