用C#获取webbrowser控件中HTML元素的绝对位置 [英] getting absolute position of HTML element in webbrowser control with C#

查看:115
本文介绍了用C#获取webbrowser控件中HTML元素的绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能得到我使用C#加载webbrowser控件的特定HTML元素的绝对位置。



我尝试了几乎所有的选项.Net提供..
他们都没有给我正确的位置。他们都给我0为Y坐标..元素肯定不是在0 ..

有没有人有任何解决方案或想法来解决这个问题?

解决方案

这是我到目前为止的解决方案:
$ b


//将网页浏览器的大小设置为与图像大小相同
int width,height;
width = webBrowser1.Document.Images [0] .ClientRectangle.Width;
height = webBrowser1.Document.Images [0] .ClientRectangle.Height;

  webBrowser1.Width = width; 
webBrowser1.Height = height;

//垂直滚动到该元素
webBrowser1.Document.Images [0] .OffsetParent.ScrollIntoView(true);

//计算元素的x,y偏移量
int x = webBrowser1.Document.Images [s] .OffsetRectangle.Left +
webBrowser1.Document.Images [s] .OffsetParent.OffsetRectangle.Left +
webBrowser1.Document.Images [s] .OffsetParent.OffsetParent.OffsetRectangle.Left +
webBrowser1.Document.Images [s] .OffsetParent.OffsetParent.OffsetParent.OffsetRectangle.Left +
webBrowser1.Document.Images [s] .OffsetParent.OffsetParent.OffsetParent.OffsetParent.OffsetRectangle.Left;

int y = webBrowser1.Document.GetElementsByTagName(HTML)[0] .ScrollTop;

//现在滚动到那个元素
webBrowser1.Document.Window.ScrollTo(x,y);



现在这个代码完美地工作..但计算偏移有一个问题。我需要计算元素的offsetparent,然后计算offsetparent的offsetparent等。我需要动态地不添加它一个接一个..我不知道该怎么做。任何想法?

编辑:
这里是我的最后和最终版本,它可以与任何html元素一起使用,它会找到绝对

  public int getXoffset(HtmlElement el)
{
// get元素pos
int xPos = el.OffsetRectangle.Left;

//获取父母pos
HtmlElement tempEl = el.OffsetParent;
while(tempEl!= null)
{
xPos + = tempEl.OffsetRectangle.Left;
tempEl = tempEl.OffsetParent;
}

return xPos;
}

public int getYoffset(HtmlElement el)
{
// get元素pos
int yPos = el.OffsetRectangle.Top;

//获取父母pos
HtmlElement tempEl = el.OffsetParent;
while(tempEl!= null)
{
yPos + = tempEl.OffsetRectangle.Top;
tempEl = tempEl.OffsetParent;
}

返回yPos;
}

然后在以下位置使用:

  //现在滚动到那个元素
webBrowser1.Document.Window.ScrollTo(x,y);

完成!


I was wondering if its possible to get the absolute position of specific HTML element I have loaded in webbrowser control with C#.

I tried almost all of the options that .Net provides.. none of them give me the correct position. all of them give me 0 for Y coordinate.. the element is definitely is not in 0..

does anybody have any solution or idea to solve this?

解决方案

here is the solution I got so far:

// set the size of our web browser to be the same size as the image int width, height; width = webBrowser1.Document.Images[0].ClientRectangle.Width; height = webBrowser1.Document.Images[0].ClientRectangle.Height;

webBrowser1.Width = width;
webBrowser1.Height = height;

//scroll vertically to that element
webBrowser1.Document.Images[0].OffsetParent.ScrollIntoView(true);

//calculate x, y offset of the element
int x = webBrowser1.Document.Images[s].OffsetRectangle.Left + 
webBrowser1.Document.Images[s].OffsetParent.OffsetRectangle.Left + 
webBrowser1.Document.Images[s].OffsetParent.OffsetParent.OffsetRectangle.Left+
webBrowser1.Document.Images[s].OffsetParent.OffsetParent.OffsetParent.OffsetRectangle.Left+
webBrowser1.Document.Images[s].OffsetParent.OffsetParent.OffsetParent.OffsetParent.OffsetRectangle.Left;

int y = webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop;

//now scroll to that element
webBrowser1.Document.Window.ScrollTo(x, y);

now this code works perfectly.. but there is an issue with calculating the offsets. I need to calculate the offsetparent of the element then calculate the offsetparent of the offsetparent etc.. I need to do that dynamically not adding it one by one.. I don't know how to do that. any ideas?

EDIT: here is my last and final version and it works with any html element it will find the absolute position of any element I want..

   public int getXoffset(HtmlElement el)
     {
         //get element pos
         int xPos = el.OffsetRectangle.Left;

         //get the parents pos
         HtmlElement tempEl = el.OffsetParent;
         while (tempEl != null)
         {
             xPos += tempEl.OffsetRectangle.Left;
             tempEl = tempEl.OffsetParent;
         }

         return xPos; 
     }  

     public int getYoffset(HtmlElement el)
     {
         //get element pos
         int yPos = el.OffsetRectangle.Top;

         //get the parents pos
         HtmlElement tempEl = el.OffsetParent;
         while (tempEl != null)
         {
             yPos += tempEl.OffsetRectangle.Top;
             tempEl = tempEl.OffsetParent;
         }

         return yPos;
     }

then use the position with:

 //now scroll to that element
 webBrowser1.Document.Window.ScrollTo(x, y);

done!

这篇关于用C#获取webbrowser控件中HTML元素的绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆