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