webbrowser 选取区域

本文详细介绍了如何在HTML文档中获取和设置反选区域,包括使用相关接口和方法来实现反选操作。

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

    

1 反选区域的获取
// pHTMLDocument2:网页加载完成后的doc2
// lStart,lStop,将返回区域起始点和结束点
// 函数返回反选区域的长度
long GetSelRange(CComQIPtr<IHTMLDocument2> pHTMLDocument2,long& lStart,long& lStop)
{
 long lResult=-1;
 CComBSTR bstrText;

 CComQIPtr<IHTMLSelectionObject> pHTMLSelectionObject;
 CComQIPtr<IMarkupServices> pMarkupServices;
 CComQIPtr<IHTMLTxtRange> pHTMLTxtRange;

 CComQIPtr<IMarkupPointer> pMarkupPointer1;
 CComQIPtr<IMarkupPointer> pMarkupPointer2;
 CComQIPtr<IMarkupPointer2> pMarkupPointer21;
 CComQIPtr<IMarkupPointer2> pMarkupPointer22;
 CComQIPtr<IDispatch> pDispatch;

 if (pHTMLDocument2)
 {
  // 获得选中文档的区域接口
  pHTMLDocument2->get_selection(&pHTMLSelectionObject);
  if (pHTMLSelectionObject)
  {
   pHTMLSelectionObject->createRange(&pDispatch);
   if (pDispatch)
   {
    pDispatch->QueryInterface(IID_IHTMLTxtRange,(void**)&pHTMLTxtRange);
   }
  }

  // 获得开始和结尾接口
  pHTMLDocument2->QueryInterface(IID_IMarkupServices, (void**)&pMarkupServices);
  if (pHTMLTxtRange&&pMarkupServices)
  {
   pMarkupServices->CreateMarkupPointer(&pMarkupPointer1);
   pMarkupServices->CreateMarkupPointer(&pMarkupPointer2);

   if (pMarkupPointer1&&pMarkupPointer2)
   {
    pMarkupServices->MovePointersToRange(pHTMLTxtRange,
     pMarkupPointer1, pMarkupPointer2);
    pMarkupPointer1->QueryInterface(IID_IMarkupPointer2,(void**)&pMarkupPointer21);
    pMarkupPointer2->QueryInterface(IID_IMarkupPointer2,(void**)&pMarkupPointer22);
   }
  }

  // 找出开始和结尾的偏移
  if (pMarkupPointer21&&pMarkupPointer22)
  {
   pMarkupPointer21->GetMarkupPosition(&lStart);
   pMarkupPointer22->GetMarkupPosition(&lStop);

//   pHTMLTxtRange->get_text(&bstrText);
   lResult=lStop-lStart;
  }
 }

 return lResult;
}

 

2 反选区域的设置

// pHTMLDocument2:网页加载完成后的doc2
// lStart,lStop:设置的反选区域起始点和结束点

BOOL SetSelRange(CComQIPtr<IHTMLDocument2> pHTMLDocument2,long lStart,long lStop)
{
 BOOL bResult=FALSE;

 CComQIPtr<IHTMLElement>  pHTMLElement;
 CComQIPtr<IHTMLBodyElement> pHTMLBodyElement;
 CComQIPtr<IHTMLTxtRange> pHTMLTxtRange;

 CComQIPtr<IMarkupServices> pMarkupServices;
 CComQIPtr<IMarkupPointer> pMarkupPointer1;
 CComQIPtr<IMarkupPointer> pMarkupPointer2;
 CComQIPtr<IMarkupPointer2> pMarkupPointer21;
 CComQIPtr<IMarkupPointer2> pMarkupPointer22;

 if (pHTMLDocument2)
 {
  // 创建一个区域接口
  pHTMLDocument2->get_body(&pHTMLElement);
  if (pHTMLElement)
  {
   pHTMLElement->QueryInterface(IID_IHTMLBodyElement,(void**)&pHTMLBodyElement);
   if (pHTMLBodyElement)
   {
    pHTMLBodyElement->createTextRange(&pHTMLTxtRange);
   }
  }

  // 获得起点和结束点的接口
  pHTMLDocument2->QueryInterface(IID_IMarkupServices, (LPVOID*)&pMarkupServices);
  if (pHTMLTxtRange&&pMarkupServices)
  {
   pMarkupServices->CreateMarkupPointer(&pMarkupPointer1);
   pMarkupServices->CreateMarkupPointer(&pMarkupPointer2);
   if (pMarkupPointer1&&pMarkupPointer2)
   {
    pMarkupServices->MovePointersToRange(pHTMLTxtRange,
     pMarkupPointer1, pMarkupPointer2);

    pMarkupPointer1->QueryInterface(IID_IMarkupPointer2,(void**)&pMarkupPointer21);
    pMarkupPointer2->QueryInterface(IID_IMarkupPointer2,(void**)&pMarkupPointer22);
   }
  }

  if (pMarkupPointer21&&pMarkupPointer22)
  {
   pMarkupPointer21->MoveToMarkupPosition(NULL,lStart);
   pMarkupPointer22->MoveToMarkupPosition(NULL,lStop);
   pMarkupServices->MoveRangeToPointers(pMarkupPointer21,
    pMarkupPointer22,pHTMLTxtRange);
   pHTMLTxtRange->select();
  }
 }

 return bResult;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值