8 Replies - 4067 Views - Last Post: 06 December 2012 - 09:04 PM Rate Topic: -----

#1 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Get absolute location of an html element in webbrowser control

Posted 25 November 2012 - 06:39 PM

Hey there,

Is it possible to get the location (relative to the screen) of a picture or other html element inside the webbrowser control?

This is what i've tried, I found it online.

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


Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Get absolute location of an html element in webbrowser control

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Get absolute location of an html element in webbrowser control

Posted 25 November 2012 - 08:25 PM

View PostTechKid, on 25 November 2012 - 07:39 PM, said:

Is it possible to get the location (relative to the screen) of a picture or other html element inside the webbrowser control?

Function GetXOffSet(ByVal elem As HtmlElement) As Integer
 '.....
    Function GetYOffSet(ByVal elem As HtmlElement) As Integer
 '.....


What kind of element are you passing to these functions?

What happens when you try it? What do the functions return, if anything?
Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Get absolute location of an html element in webbrowser control

Posted 25 November 2012 - 09:03 PM

Actually, I just tried it with "img" tags, and your functions work just fine for offset within the browser.

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("file:///J:/AuroraMonhelp.html")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        For Each ele As HtmlElement In WebBrowser1.document.GetElementsByTagName("img")
            ListBox1.Items.Add(ele.GetAttribute("src"))
            ListBox1.Items.Add(GetXOffSet(ele))
            ListBox1.Items.Add(GetYOffSet(ele))
        Next
    End Sub

    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

End Class


Of course, the elements might be anywhere on or off the screen, visible or not visible, depending on:
  • The offset of the element
  • The size of the browser window
  • The amount the browser window is scrolled.

Was This Post Helpful? 0
  • +
  • -

#4 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Re: Get absolute location of an html element in webbrowser control

Posted 01 December 2012 - 07:15 PM

View Postlar3ry, on 26 November 2012 - 04:03 AM, said:

Spoiler


I appreciate the response, and greatly apologize for a delayed reply on my part.

After having forgot about this topic, later testing it, and now coming back to this thread i've realized that i does work in some scenarios; those being when the WebBrowser control is fully docked, and the window maximized, and even then the scroll will be off.

I'm looking for a way to get the absolute position of an html control when the webbrowser control isn't fully docked and the window not necessarily being maximized.

This post has been edited by TechKid: 01 December 2012 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Get absolute location of an html element in webbrowser control

Posted 01 December 2012 - 09:20 PM

View PostTechKid, on 01 December 2012 - 08:15 PM, said:

After having forgot about this topic, later testing it, and now coming back to this thread i've realized that i does work in some scenarios; those being when the WebBrowser control is fully docked, and the window maximized, and even then the scroll will be off.

You have to take into account: the Left and Top "margins" of the WebBrowser, but basically, if the control is visible, and the contents have not been scrolled, the position of the control should be:

Form.Left + WebBrowser1.Left + WebBrowser1.<leftmargin> + control.Left
Form.Top + WebBrowser1.Top + WebBrowser1.<topmargin> + control.Top


I'm not sure about how to get the margins. I'll look for ideas on that.

Quote

I'm looking for a way to get the absolute position of an html control when the webbrowser control isn't fully docked and the window not necessarily being maximized.

If I may ask, are you by any chance trying to interact with a control on a WebBrowser? Perhaps to fill in an input box, click a Button or a RadioButton? If so, are you aware that there are other ways to do that? Let me know, because I just recently wrote a small program to do all those three operations. If not, could you tell me what it is you are trying to do? Perhaps there are alternative methods.
Was This Post Helpful? 0
  • +
  • -

#6 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Re: Get absolute location of an html element in webbrowser control

Posted 02 December 2012 - 09:33 PM

View Postlar3ry, on 02 December 2012 - 04:20 AM, said:

Spoiler


I actually figured out a better alternative for what i needed to do:

WebBrowser1.document.GetElementById("idObject").ScrollIntoView(true) 'true to allign with top of webbrowser, filling out the webbrowser control
'from here, just getting the webbrowser's location pointed to the screen will do the job.
'i dont have the code in front of me and i can't quite remember the syntax.



As a matter of fact, I am trying to interact with the control, but the control is a flash object.

This post has been edited by TechKid: 05 December 2012 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#7 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Re: Get absolute location of an html element in webbrowser control

Posted 05 December 2012 - 03:24 PM

Do you perhaps know of a better way to interact with a flash object, besides clicking at a point?
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Get absolute location of an html element in webbrowser control

Posted 05 December 2012 - 08:44 PM

View PostTechKid, on 05 December 2012 - 04:24 PM, said:

Do you perhaps know of a better way to interact with a flash object, besides clicking at a point?

No, I don't. From a Google search, it would appear that there are things you can put into a .swf file that allows it to be interacted with from VB.Net, though it almost sounds like it does so only for local copies of the files.

You might have to go back to refining your mouse positioning and sending a Click with SendKeys or the like.
Was This Post Helpful? 0
  • +
  • -

#9 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Re: Get absolute location of an html element in webbrowser control

Posted 06 December 2012 - 09:04 PM

View Postlar3ry, on 06 December 2012 - 03:44 AM, said:

View PostTechKid, on 05 December 2012 - 04:24 PM, said:

Do you perhaps know of a better way to interact with a flash object, besides clicking at a point?

No, I don't. From a Google search, it would appear that there are things you can put into a .swf file that allows it to be interacted with from VB.Net, though it almost sounds like it does so only for local copies of the files.

You might have to go back to refining your mouse positioning and sending a Click with SendKeys or the like.


Alright, thank you very much. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1