现在的位置: 首页 > 综合 > 正文

webbrowser 选取区域

2014年01月28日 ⁄ 综合 ⁄ 共 3227字 ⁄ 字号 评论关闭

    

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

 

抱歉!评论已关闭.