0

我正在开发一个可视化 C++ 应用程序(x64)。实际上我想做的是假设我们在窗口资源管理器中有一个 html 文件(我的意思是文件扩展名为“.html”的文件)。当我们单击它时,我们会在预览窗格中获得它的预览(因此我们不需要打开此文件,只需单击文件即可在预览窗格中看到该文件)。

我开发了一种类似类型的应用程序,但在我的情况下,当我单击“html 文件”时,我只是在预览窗格中获取该 html 文件的代码(如果您在记事本中打开该 html 文件,您可以看到该代码)。这预计不会发生,但我想预览那个“html 文件”而不是那个 html 文件的代码。

我想我需要托管一些浏览器控件,它将我在预览窗格中的 html 代码转换为 html 文件的显示(如果我是正确的???)怎么做?

这是我的代码-

    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;

}

任何想法为什么我的窗口中有 html 代码?在代码中做什么才能在我的窗口中预览 html 文件而不是 html 代码?如果对理解我有任何疑问,请告诉我??

4

1 回答 1

1

您的窗口中有 html 代码,因为您选择了 Richedit 作为文本渲染器,并且您的文本没有以“{\rtf”开头。

如果你想要 html 显示,你需要一个 html 渲染器而不是富编辑,比如 MFC 的 CHtmlEditCtrl。如果您不想使用 MFC,您可以编写一个 ActiveX 容器来直接托管 webbrowser 控件

于 2013-08-07T22:14:08.703 回答