how to host html control in my window using a buffer which contents a html file

292 Views Asked by At

I am developing a visual c++ applicatio(x64). what actually i am trying to do is that suppose we have a html file in window explorer(i mean file with file extension ".html"). when we single click on it we get its preview on preview pane(so we don't need to open this file we can see the file in preview pane on a single click to a file).

I have developed a similar type of application but in my case when i click on the "html file" i just get the code of that html file in preview pane(the code which you can see if you open that html file in notepad). which is not expected to happen but I want to have the preview of that "html file" not the code of that html file.

I think i need to host some browser control which will transform my html code in preview pane to the display of html file(If i am correct ???) How to do that ??

Here is my code for that-

    IHTMLDocument2  * pDoc=NULL;
    HRESULT hr2 = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (LPVOID *) &pDoc);
    if (pDoc)
    {
        IPersistStreamInit *pPersist = NULL;
        pDoc->QueryInterface(IID_IPersistStreamInit,(LPVOID *) &pPersist);
        if (pPersist)
        {
            IMarkupServices *pMS = NULL;
            pPersist->InitNew();
            pPersist->Release();
            pDoc->QueryInterface(IID_IMarkupServices,(LPVOID *) &pMS);
            if (pMS)
            {
                IMarkupContainer *pMC = NULL;
                IMarkupPointer *pMkStart = NULL;
                IMarkupPointer *pMkFinish = NULL;
                pMS->CreateMarkupPointer(&pMkStart);
                pMS->CreateMarkupPointer(&pMkFinish);
                pMS->ParseString(HtmlFileContents,0,&pMC,pMkStart,pMkFinish);
    //this HtmlFileContents is actually a buffer of olechar type which  contains the code of html file (the code which you see when you open the html file in notepad)
                if (pMC)
                {
                    IHTMLDocument2 *pNewDoc = NULL;
                    pMC->QueryInterface(IID_IHTMLDocument,(LPVOID *) &pNewDoc);
                    if (pNewDoc)
                    {
                        IHTMLElement *pBody;
                        pNewDoc->get_body(&pBody);
                        if (pBody)
                        {
                            BSTR strText;
                            pBody->get_innerText(&strText);
                            hr = instance->CreatePreviewWindowForHtml(strText); // this function is responsible for displaying the html file in window. you can see its definition below after this code.
                            SysFreeString(strText);
                            pBody->Release();
                        }
                        pNewDoc->Release();
                    }
                    pMC->Release();
                }
                if (pMkStart)
                    pMkStart->Release();
                if (pMkFinish)
                    pMkFinish->Release();
                pMS->Release();
                pMS->Release();
            }
        }
        pDoc->Release();
    }
    return true;

 and the function defintion of CreatePreviewWindowForHtml() is as below-    
CreatePreviewWindowForHtml(PCWSTR pszRtfWide)
{
    assert(m_hwndPreview3 == NULL);
    HRESULT hr = E_FAIL;

    if (m_hwndPreview3 == NULL)
    {
        HRESULT hr5 = HRESULT_FROM_WIN32(GetLastError());
    }

    if (m_hinstEditLibrary == NULL)
    {
        // MSFTEDIT_CLASS used below comes from this binary
        m_hinstEditLibrary = LoadLibraryW(L"msftedit.dll");
    }

    if (m_hinstEditLibrary == NULL)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
    }
    else
    {
        // Create the preview window
        HWND pr = m_hwndPreview3 = CreateWindowExW(0, MSFTEDIT_CLASS, NULL,
            WS_CHILD | WS_VSCROLL | WS_VISIBLE | ES_MULTILINE | ES_READONLY,  // Always create read-only
            m_rcParent.left, m_rcParent.top, RECTWIDTH(m_rcParent), RECTHEIGHT(m_rcParent),
            m_hwndPreview, NULL, NULL,NULL);
        if (m_hwndPreview3 == NULL)
        {
            MessageBoxA(m_hwndPreview3,strerror(hr),"BTN WND2", MB_ICONINFORMATION);
        }
        else
        {
            int const cchRtf = 1 + wcslen(pszRtfWide);
            PSTR pszRtf = (PSTR)CoTaskMemAlloc(cchRtf);
            if (pszRtf)
            {
                WideCharToMultiByte(CP_ACP, 0, pszRtfWide, cchRtf, pszRtf, cchRtf, NULL, NULL);
                SETTEXTEX st = { ST_DEFAULT, CP_ACP };

                LRESULT hr4=SendMessage(m_hwndPreview3, EM_SETTEXTEX, (WPARAM) &st, (LPARAM) pszRtfWide);
                if (SUCCEEDED(hr4))
                {
                    hr = AdjustDialogPositionAndSize();
                    SendMessage(m_hwndPreview3, EM_SETTEXTEX, (WPARAM) &st, (LPARAM) pszRtfWide);
                }


                CoTaskMemFree(pszRtf);
                hr = S_OK;
            }
            else
            {
                hr = E_OUTOFMEMORY;
            }
        }
    }
    return hr;

}

Any ideas why i have the html code in my window ?? What to do in the code in order to get the preview of html file in my window rather then html code ?? Please tell me if any doubts in understanding me ??

1

There are 1 best solutions below

2
On

You have the html code in your window because you choose a richedit as the text renderer and your text did not start with "{\rtf".

If you want html display, you need an html renderer instead of a rich edit, something like MFC's CHtmlEditCtrl. If you don't want to use MFC you can write an ActiveX container to host the webbrowser control directly.