基于ie内核的网页高亮查找2

本文介绍了一种通过调用IE内核接口IHTMLTxtRange进行网页文本高亮查找的方法。首先,通过IHTMLDocument2的get_body获取IHTMLElement,再QueryInterface得到IHTMLBodyElement并调用createTextRange。然后使用findText方法查找文本,找到后通过pasteHTML插入标记以实现高亮。同时,文章提到了如何处理隐藏元素以及在frame中递归查找的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网页高亮查找还有一种方法,就是调用内核的接口IHTMLTxtRange的方法

要获取到接口 IHTMLTxtRange,需要调用接口IHTMLBodyElement的方法 createTextRange。可采用如下过程:

1.采用IHTMLDocument2接口的get_body方法获取到IHTMLElement接口

2.采用IHTMLElement接口QueryInterface获取到接口IHTMLBodyElement;

3.采用IHTMLBodyElement的方法createTextRange,获取到IHTMLTxtRange

 

获取到IHTMLTxtRange,就可以查找了,调用findText方法,如果要对查找到的元素高亮的话,需要采用IHTMLTxtRange的方法pasteHTML

 

附上代码

void CSearchText::SearchText(IHTMLDocument2* pDoc2,SEARCH_INFO1& _info)
{
 CComPtr<IHTMLElement> lpBodyElm;
 CComPtr<IHTMLBodyElement> lpBody;

 HRESULT hr = pDoc2->get_body(&lpBodyElm);
 if (FAILED(hr))
 {
  return;
 }

 hr = lpBodyElm->QueryInterface(IID_IHTMLBodyElement,(void**)&lpBody);

 if (FAILED(hr))
 {
  return;
 }

 hr = lpBody->createTextRange(&_info.m_pTxtRange);

 if (FAILED(hr))
 {
  return;
 }
 
 long t;
 VARIANT_BOOL bFound;
 while(_info.m_pTxtRange->findText(m_bszSearchText,0,0,&bFound),bFound)
 {
  CComPtr<IHTMLElement> pElement;
  _info.m_pTxtRange->parentElement(&pElement);
 
  if (CheckElementHide(pElement))
  {
   _info.m_pTxtRange->moveStart((BSTR)CComBSTR("Character"),1,&t);
   _info.m_pTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t);
   continue;
  }

  InsertMarkup(_info.m_pTxtRange);


  _info.m_pTxtRange->moveStart((BSTR)CComBSTR("Character"),1,&t);
  _info.m_pTxtRange->moveEnd((BSTR)CComBSTR("Textedit"),1,&t);

  m_nCount ++;
  _info.m_TotalCount++;

 }

}

 

bool CSearchText::CheckElementHide(IHTMLElement* pElement)
{
 if( pElement != NULL )
 {

  for ( ; ;)
  {

   CComPtr<IHTMLElement> pParentElement = NULL;

   if (S_OK != pElement->get_parentElement(&pParentElement) || NULL == pParentElement)
    break;

   CComPtr<IHTMLCurrentStyle> pHtmlStyle = NULL;

   CComQIPtr<IHTMLElement2, &IID_IHTMLElement2> pIHtmlElement2(pElement);

   pIHtmlElement2->get_currentStyle(&pHtmlStyle);

   CComBSTR bszDisplay, bszVisible;

   if( pHtmlStyle != NULL )
   {
    pHtmlStyle->get_display(&bszDisplay);
    pHtmlStyle->get_visibility(&bszVisible);
   }

   if (0 == StrCmpI( _T("none") , bszDisplay) || 0 == StrCmpI(_T("hidden") , bszVisible) )
   {
    return true;
   }

   pElement = pParentElement;
  }
 }


 return false;

 

 

void CSearchText::InsertMarkup(CComPtr<IHTMLTxtRange> pTxtRange, bool bfocus)
{
 CComBSTR html;
 CComBSTR newhtml;

 if (bfocus)
 {
  pTxtRange->scrollIntoView(TRUE);

  newhtml.Append("<font id='AliSearch' style='background-color:#00ff00'>");
 }
 else
 {

  newhtml.Append("<font id='AliSearch' style='background-color:#fb9131'>");
 }

 //添加背景色
 pTxtRange->get_htmlText(&html);

 if(m_bszSearchText==" ")
 {
  newhtml.Append("&nbsp;"); // doesn't work very well, but prevents (some) shit
 }
 else
 {
  newhtml.AppendBSTR(html);
 }

 newhtml.Append("</font>");

 pTxtRange->pasteHTML(newhtml);
}

 

 

碰上有frame的网页,需要递归出frame中的doc,然后再按照这样的方式查找即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值