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

后端 未结 7 1271
萌比男神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:08

    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
    

提交回复
热议问题