我使用 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->write(safe_array) 语句之后添加了 m_document->execCommand("Refresh",...) ,就像我从上下文菜单 Ctrl-F 执行“刷新”时一样,按预期工作。这解决了“焦点问题”。