Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

c#

position

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?

like image 980
Desolator Avatar asked May 10 '11 18:05

Desolator


2 Answers

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!

like image 60
Desolator Avatar answered Oct 24 '22 22:10

Desolator


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;
like image 30
Alex from Jitbit Avatar answered Oct 24 '22 22:10

Alex from Jitbit