Question

I'm generating HTML pages from strings stored in a database by using the IHTMLDocument2 write(SAFEARRAY) method. This works OK. When CTRL+F is pressed the Find dialog appears as expected, but there are never any matches. What is being searched by CTRL+F? Perhaps an object is missing(that I must create) that the search looks at? Here's some relevant code:

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);
}

Answer: As @Yahia suggested, it was a focus problem. I added m_document->execCommand("Refresh",...) after the m_document->write(safe_array) statement, as when I did "refresh" from the context menu Ctrl-F worked as expected. That fixed the "focus issue".

Was it helpful?

Solution

CTRL+F is focus-aware... you need to call focus on the parentWindow of m_document after WriteHTMLString(); and/or SetWindowSize(512,384);...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top