簡體   English   中英

如何使用包含HTML文件的緩沖區將HTML控件托管在我的窗口中

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

我正在開發視覺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文件,在代碼中做什么? 如果對我有任何疑問,請告訴我?

您的窗口中有html代碼,因為您選擇了richedit作為文本渲染器,並且文本的開頭不是“ {\\ rtf”。

如果要顯示html,則需要html渲染器而不是豐富的編輯,例如MFC的CHtmlEditCtrl。 如果您不想使用MFC,則可以編寫ActiveX容器直接托管Webbrowser控件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM