Search code examples
c#htmlmshtml

get an html element top and left properties data


i'm using the mshtml dll to develop a helper to ie, i'm trying to get the position of an htmll element, i have an object with type of HTMLAnchorElementClass when i'm trying to get his style.posTop value i get a null ref exception

is there a better way to do it?

maybe other cast?

please help


Solution

  • Here's an example I found (the way you're obtaining a reference to your element object is probably different, but take a look at this anyway:

    Element = <however your get your element>;
    
    //--- Determine real element size and position relative to the main page.
    int ElementLeft = Element.offsetLeft;
    int ElementTop = Element.offsetTop;
    mshtml.IHTMLElement TmpElem = Element.offsetParent;
    while (TmpElem != null)
    {
         ElementLeft = ElementLeft + TmpElem.offsetLeft;
         ElementTop = ElementTop + TmpElem.offsetTop;
         TmpElem = TmpElem.offsetParent;
    }