getting absolute position of HTML element in webbrowser control with C#

后端 未结 7 1246
萌比男神i
萌比男神i 2021-02-04 07:39

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

相关标签:
7条回答
  • 2021-02-04 08:13

    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;
    
    0 讨论(0)
提交回复
热议问题