Using CHtmlView

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

Thanks Dmitry

+3
source share
3 answers

It is not possible to simply generate HTML in a memory line and paste it into a CHtmlView.

Our solution (which works very well) is to create a temporary html file (in the Windows temporary files directory) and go to the CHtml View for this file. Basically:

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

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

( , stdlib ++.

+1

- .

"" html- html.

html- , html.

html- - :

    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();
    }

.

+2

The solution is very simple.

Wait for the document to load by overloading the 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();
}
0
source

All Articles