使用 C# 获取网页浏览器控件中 HTML 元素的绝对位置

发布于 2024-11-05 22:30:39 字数 157 浏览 0 评论 0原文

我想知道是否可以使用 C# 获取我在 web 浏览器控件中加载的特定 HTML 元素的绝对位置。

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

捶死心动 2024-11-12 22:30:39

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


// 将网络浏览器的大小设置为与图像大小相同
int 宽度、高度;
宽度 = 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);

现在这段代码可以完美运行..但是计算偏移量时存在问题。我需要计算元素的 offsetparent,然后计算 offsetparent 的 offsetparent 等。我需要动态地执行此操作,而不是一一添加它。我不知道该怎么做。有什么想法吗?

编辑:
这是我的最后一个也是最终版本,它适用于任何 html 元素,它会找到我想要的任何元素的绝对位置..

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

然后使用该位置:

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

完成!

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!

℉絮湮 2024-11-12 22:30:39

我喜欢以前的答案,但迭代父对象两次并不是很有效。请记住 - 您在这里使用 COM/ActiveX。这工作得更快:

public Point GetOffset(HtmlElement el)
{
    //get element pos
    Point pos = new Point(el.OffsetRectangle.Left, el.OffsetRectangle.Top);

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

    return pos;
}

然后

var point = GetOffset(element);
var x = point.X;
var y = point.Y;

I like previous answers but iterating through parent objects twice is not very effective. Remember - you're working with COM/ActiveX here. This works much faster:

public Point GetOffset(HtmlElement el)
{
    //get element pos
    Point pos = new Point(el.OffsetRectangle.Left, el.OffsetRectangle.Top);

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

    return pos;
}

and then

var point = GetOffset(element);
var x = point.X;
var y = point.Y;
酒几许 2024-11-12 22:30:39

谢谢,它就像一个魅力。我不得不将其重写为VB,只是想分享解决方案:

Function GetXOffSet(ByVal elem As HtmlElement) As Integer
    Dim xPos As Integer = elem.OffsetRectangle.Left
    Dim tElm As HtmlElement = elem.OffsetParent
    Dim trig As Boolean = False
    While Not trig
        Try
            xPos += tElm.OffsetRectangle.Left
            tElm = tElm.OffsetParent
        Catch ex As Exception
            trig = True
        End Try
    End While
    Return xPos
End Function

Function GetYOffSet(ByVal elem As HtmlElement) As Integer
    Dim yPos As Integer = elem.OffsetRectangle.Top
    Dim tElm As HtmlElement = elem.OffsetParent
    Dim trig As Boolean = False
    While Not trig
        Try
            yPos += tElm.OffsetRectangle.Top
            tElm = tElm.OffsetParent
        Catch ex As Exception
            trig = True
        End Try
    End While
    Return yPos
End Function

Thank you, it works like a charm. I had to rewrite it as VB, and just want to share the solution:

Function GetXOffSet(ByVal elem As HtmlElement) As Integer
    Dim xPos As Integer = elem.OffsetRectangle.Left
    Dim tElm As HtmlElement = elem.OffsetParent
    Dim trig As Boolean = False
    While Not trig
        Try
            xPos += tElm.OffsetRectangle.Left
            tElm = tElm.OffsetParent
        Catch ex As Exception
            trig = True
        End Try
    End While
    Return xPos
End Function

Function GetYOffSet(ByVal elem As HtmlElement) As Integer
    Dim yPos As Integer = elem.OffsetRectangle.Top
    Dim tElm As HtmlElement = elem.OffsetParent
    Dim trig As Boolean = False
    While Not trig
        Try
            yPos += tElm.OffsetRectangle.Top
            tElm = tElm.OffsetParent
        Catch ex As Exception
            trig = True
        End Try
    End While
    Return yPos
End Function
不乱于心 2024-11-12 22:30:39

有一种直接获取坐标的方法。 IHTMLElement2 具有 getBoundingClientRect 方法,可以为您提供元素的坐标。

IHTMLDocument3 doc = (IHTMLDocument3)this.webbrowser.Document;
IHTMLElement2 element = (IHTMLElement2)doc.getElementById(idElement);
IHTMLRect rect = element.getBoundingClientRect();

int x = rect.left;
int y= rect.top;

There is a direct way to get the coordinates. IHTMLElement2 has the method getBoundingClientRect that gives you the coordinates of the element.

IHTMLDocument3 doc = (IHTMLDocument3)this.webbrowser.Document;
IHTMLElement2 element = (IHTMLElement2)doc.getElementById(idElement);
IHTMLRect rect = element.getBoundingClientRect();

int x = rect.left;
int y= rect.top;
对你而言 2024-11-12 22:30:39

只是根据我的需要分享一个稍微不同的实现来获取绝对定位矩形:

public Rectangle GetAbsoluteRectangle(HtmlElement element) {
    //get initial rectangle
    Rectangle rect = element.OffsetRectangle;

    //update with all parents' positions
    HtmlElement currParent = element.OffsetParent;
    while (currParent != null) {
            rect.Offset(currParent.OffsetRectangle.Left, currParent.OffsetRectangle.Top);
            currParent = currParent.OffsetParent;
    }

    return rect;
}

Just sharing a slightly different implementation based on my needs to get the absolute positioning rectangle:

public Rectangle GetAbsoluteRectangle(HtmlElement element) {
    //get initial rectangle
    Rectangle rect = element.OffsetRectangle;

    //update with all parents' positions
    HtmlElement currParent = element.OffsetParent;
    while (currParent != null) {
            rect.Offset(currParent.OffsetRectangle.Left, currParent.OffsetRectangle.Top);
            currParent = currParent.OffsetParent;
    }

    return rect;
}
杯别 2024-11-12 22:30:39

不知道为什么,但 offsetparent 并不总是返回(IE11 的情况,地图对象)
当未提供parentOffset时,我必须在循环中添加parentElement

    While parent IsNot Nothing
            y += parent.offsetTop
            x += parent.offsetLeft
            If  parent.offsetParent IsNot Nothing Then
                parent = parent.offsetParent  
            Else
                parent = parent.parentElement
            End If
    End While

并且您需要添加IE浏览器位置,菜单空间和边框...

Not sure why but offsetparent is not allways returned (case of IE11, map object)
I had to add parentElement in the loop when parentOffset is not provided

    While parent IsNot Nothing
            y += parent.offsetTop
            x += parent.offsetLeft
            If  parent.offsetParent IsNot Nothing Then
                parent = parent.offsetParent  
            Else
                parent = parent.parentElement
            End If
    End While

And you need to add IE browser position, the menu space and borders ...

千年*琉璃梦 2024-11-12 22:30:39

对我来说最干净的方法如下:

HtmlElement elem = webBrowser.Document.GetElementById(idElement);
IHTMLRect 矩形 = ((IHTMLElement2) elem.DomElement).getBoundingClientRect();
// rect.top 和 rect.left 代表绝对坐标。

The cleanest way that works for me is the following:

HtmlElement elem = webBrowser.Document.GetElementById(idElement);
IHTMLRect rect = ((IHTMLElement2) elem.DomElement).getBoundingClientRect();
// rect.top and rect.left represent absolute coordinates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文