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

后端 未结 7 1244
萌比男神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 07:53

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

提交回复
热议问题