如何使用包含 html 文件的缓冲区在我的窗口中托管 html 控件

Posted

技术标签:

【中文标题】如何使用包含 html 文件的缓冲区在我的窗口中托管 html 控件【英文标题】:how to host html control in my window using a buffer which contents a html file 【发布时间】:2013-08-07 12:25:11 【问题描述】:

我正在开发一个可视化 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 代码? 如果对理解我有任何疑问,请告诉我??

【问题讨论】:

没有人对这个问题感兴趣..这非常令人惊讶...... 【参考方案1】:

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

如果你想要 html 显示,你需要一个 html 渲染器而不是富编辑,比如 MFC 的 CHtmlEditCtrl。如果不想使用 MFC 可以write an ActiveX container to host the webbrowser control directly。

【讨论】:

..我正在使用 visualc++(X64)。我应该使用什么 html 渲染器?我不得不使用 MFC(我没有更多选择,因为已经完成了一半以上的工作,这是我被卡住的最后一部分。)。 查看 Visual C++ 文档中的 HTMLEdit 示例。

以上是关于如何使用包含 html 文件的缓冲区在我的窗口中托管 html 控件的主要内容,如果未能解决你的问题,请参考以下文章

电子:从缓冲区加载URL

如何在我的 Android 应用程序中读取 pdf 文件 [重复]

在 Node 中使用“fs”,我将如何流式传输 CSV 文件?

如何在屏幕外窗口中呈现焦点指示器?

如何将文件缓冲区转换为 <img> 标签 src?

如何使用 fread 和 fwrite 函数读写二进制文件?