出现IWebBrowser2 IHTMLDocument2 CTRL + F对话框,但找不到匹配项

时间:2011-09-11 16:22:24

标签: iwebbrowser2 ihtmldocument2

我正在使用IHTMLDocument2 write(SAFEARRAY)方法从存储在数据库中的字符串生成HTML页面。这很好用。 按下CTRL + F时,“查找”对话框按预期显示,但永远不会有任何匹配。 CTRL + F正在搜索什么?也许搜索所看到的对象缺失(我必须创建)? 这是一些相关的代码:

CComPtr<IDispatch> m_spDisp;
CComPtr<IWebBrowser2> m_spWeb2;
HRESULT m_hr;
IHTMLDocument2* m_document;

BOOL CSwiftDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    m_BackMenuButton.SetToolTipText(_T("Back"));
    m_bInitialised = true;
    m_bBackClicked = false;
    m_svURLList.clear();
    m_nCurrentPage = -1;
    m_bitBack.LoadBitmap(IDB_BACK_BITMAP);
        m_BackMenuButton.SetBitmap(m_bitBack);
    m_spGlobal.CreateInstance(__uuidof(GLOBVARSLib::Global ) ); 
    m_browser.Navigate(CSTR m_sURL, NULL, NULL, NULL, NULL);
    GetDocument();
    WriteHTMLString();
    SetWindowSize(512,384);
    return TRUE;
}



void CSwiftDlg::GetDocument()
{
    m_hr = S_OK;
    m_spDisp = m_browser.get_Application();
    if (m_spDisp != NULL && m_spWeb2 ==NULL)
    {
         m_hr = m_spDisp->QueryInterface(IID_IWebBrowser2,(void**)&m_spWeb2);
    }
    if (SUCCEEDED(m_hr) && m_spWeb2 != NULL)
    {
        // get browser document's dispatch interface
        IDispatch *document_dispatch = NULL;
        m_hr = m_spWeb2->get_Document(&document_dispatch);
        if (SUCCEEDED(m_hr) && (document_dispatch != NULL))
        {           // get the actual document interface
            m_hr = document_dispatch->QueryInterface(IID_IHTMLDocument2, (void **)&m_document);
            // release dispatch interface
            document_dispatch->Release();
        }
    }
}


void CSwiftDlg::WriteHTMLString()
{
    if (m_document == NULL)
        GetDocument();  
    SAFEARRAY *empty_array = SafeArrayCreateVector(VT_VARIANT,0,1);
    // construct text to be written to browser as SAFEARRAY
    SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);
    VARIANT *variant;
    SafeArrayAccessData(safe_array,(LPVOID *)&variant);
    variant->vt      = VT_BSTR;
    variant->bstrVal = m_sHTML.AllocSysString();
    SafeArrayUnaccessData(safe_array);
    // write SAFEARRAY to browser document
    m_document->write(empty_array);
    m_document->close();
    m_document->write(safe_array);
}

答案: 正如@Yahia所说,这是一个焦点问题。我在m_document-&gt; write(safe_array)语句之后添加了m_document-&gt; execCommand(“Refresh”,...),就像我从上下文菜单“刷新”时一样按Ctrl-F工作。这解决了“焦点问题”。

1 个答案:

答案 0 :(得分:1)

CTRL + F具有焦点感知功能......您需要在WriteHTMLString();和/或SetWindowSize(512,384);focus parentWindow上致电m_document。 ..