dpreznik
dpreznik

Reputation: 247

Using CHtmlView

In my MFC application, I display text line by line in CScrollView. Now the new requirement is to display text (and sometimes images) in html format, preserving all effects e.g. bold, italic etc. I know I can use CHtmlView to display html files, but I need to display text stored in memory line by line. Is it possible?

Thanks, Dmitriy

Upvotes: 3

Views: 6641

Answers (3)

Chebyr
Chebyr

Reputation: 2181

The solution is very straightforward

Wait for document loading to be completed by overloading OnDocumentComplete function

CHtmlView::OnDocumentComplete( LPCTSTR lpszURL)
{

IHTMLDocument2 *document = GetDocument();

IHTMLElement* pBody = document->get_body();

BSTR str = "your HTML";

pBody-> put_innerHTML(str);

document->close();
document->Release();
}

Upvotes: 2

spassig
spassig

Reputation: 1041

It's not possible to simply generate HTML in a memory string and insert it in a CHtmlView.

Our solution (which works pretty well) is to generate a temporary html file (in Windows temp directory) and navigate the CHtml View to this file. In principle:

OurTempFileClass theTempFile;
theTempFile.GetStream()->Put(mHTMLString.Get(), mHTMLString.GetLength());

CHtmlCtrl theHtmlCtrl;
theHtmlCtrl.Navigate2(theTempFile->GetFullPath());

(this is pseudo code cause we do not use stdlib c++ classes.

Upvotes: 1

Max
Max

Reputation: 3180

We do something like that for our log.

We just keep a "live" html document and append to it and redisplay it to the html view.

We have implemented a small custom html builder for our own purpose to add items to the html.

You can send a string to an html document with something like :

    IHTMLDocument2 *document = GetDocument();
    if (document != NULL) 
    {

        // construct text to be written to browser as SAFEARRAY
        SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);

        VARIANT *variant;
        // string contains the HTML data.
        // convert char* string to OLEstring

        CComBSTR bstrTmp = string;

        SafeArrayAccessData(safe_array,(LPVOID *)&variant);
        variant->vt = VT_BSTR;
        variant->bstrVal = bstrTmp;
        SafeArrayUnaccessData(safe_array);

        // write SAFEARRAY to browser document to append string
        document->write(safe_array);

        //Detach CComBSTR since string will be freed by SafeArrayDestroy
        bstrTmp.Detach();

        //free safe_array
        SafeArrayDestroy(safe_array);

        //release document
        document->Release();
    }

Max.

Upvotes: 2

Related Questions