IWebBrowser2 IHTMLDocument2 La boîte de dialogue CTRL + F apparaît mais ne trouve aucune correspondance

StackOverflow https://stackoverflow.com/questions/7379577

  •  28-10-2019
  •  | 
  •  

Question

Je génère des pages HTML à partir de chaînes stockées dans une base de données en utilisant la méthode d'écriture IHTMLDocument2 (SAFEARRAY).Cela fonctionne bien. Lorsque vous appuyez sur CTRL + F, la boîte de dialogue Rechercher apparaît comme prévu, mais il n'y a jamais de correspondance.Qu'est-ce qui est recherché par CTRL + F?Il manque peut-être un objet (que je dois créer) que la recherche examine? Voici un code pertinent:

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

Réponse: Comme @Yahia l'a suggéré, c'était un problème de concentration.J'ai ajouté m_document-> execCommand ("Refresh", ...) après l'instruction m_document-> write (safe_array), comme quand j'ai fait "refresh" depuis le menu contextuel Ctrl-F fonctionnait comme prévu.Cela a résolu le "problème de focus".

Était-ce utile?

La solution

CTRL + F prend en charge la mise au point ... vous devez appeler focus sur le parentWindow de m_document après WriteHTMLString(); et / ou SetWindowSize(512,384); ...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top