6

I have a form with web browser control, which loads a webpage ( Its working fine, the page loads ok)

Now my issue is, i want to find whether a particular url-link is below the fold or above the fold ( i mean, whether the user have to scroll down to see this link, or not ) whether this v is visible with out scrolling or we need to scroll to see it.. I hope i am clear

i have done extensive search, but looks like no info is available about find an html elements position (above or below current view)

Does anybody knows something about this and can point me in right direction please? (i am looking for c# solution - WinForms )

Update: Big thanks to John Koerner for the code. Really appreciate the time and effort he put in solving my issue.

And to Jonathan & everybody else also.. I wish i could mark Jonathans reply also as answer, but it allows only one reply to be marked as answer. His comment was also clear and useful hint. THanks you guys are great!!!

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
ugly hand
  • 219
  • 1
  • 4
  • 12

2 Answers2

7

Ok, I have tested this on google and stackoverflow and it seems to work:

private bool isElementVisible(WebBrowser web, string elementID)
{

    var element = web.Document.All[elementID];

    if (element == null)
        throw new ArgumentException(elementID + " did not return an object from the webbrowser");

    // Calculate the offset of the element, all the way up through the parent nodes
    var parent = element.OffsetParent;
    int xoff = element.OffsetRectangle.X;
    int yoff = element.OffsetRectangle.Y;

    while (parent != null)
    {
        xoff += parent.OffsetRectangle.X;
        yoff += parent.OffsetRectangle.Y;
        parent = parent.OffsetParent;
    }

    // Get the scrollbar offsets
    int scrollBarYPosition = web.Document.GetElementsByTagName("HTML")[0].ScrollTop;
    int scrollBarXPosition = web.Document.GetElementsByTagName("HTML")[0].ScrollLeft;

    // Calculate the visible page space
    Rectangle visibleWindow = new Rectangle(scrollBarXPosition, scrollBarYPosition, web.Width, web.Height);

    // Calculate the visible area of the element
    Rectangle elementWindow = new Rectangle(xoff,yoff,element.ClientRectangle.Width, element.ClientRectangle.Height);

    if (visibleWindow.IntersectsWith(elementWindow))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Then to use it, you simply call:

isElementVisible(webBrowser1, "topbar")  //StackOverflow's top navigation bar
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • wow.. awesome. Thanks John Koemer.. Really appreciate the effort and time you put in helping me. Best of luck.. Well guys, all of you are simply superb. THanks a lot!!!! – ugly hand Feb 09 '12 at 21:14
3

I have and idea that could work (never tried it, but it's the best that I can offer to you, sorry)

You can call javascripts functions in the webbrowsercontrol: LINK

You can also make javascripts functions that give you the position of an element: LINK

If you mix this two concepts, you can know if the element is visible or not, as you know the size of the webbrowsercontrol.

Note that you can inject javascript code to the webbrowsercontrol. This SO post explain how to do it: LINK

Good luck.

Community
  • 1
  • 1
Jonathan
  • 11,809
  • 5
  • 57
  • 91
  • thank you jonathan. I will sure try it. If anybody has any quick fix or simpler method, please dont hesitate to add it – ugly hand Feb 09 '12 at 20:05
  • Jonahtan, I wish i could mark your reply also as answer, but it allows only one reply to be marked as answer. Your comment was also crystal clear and useful hint. Have rated ur answer instead – ugly hand Feb 10 '12 at 03:42
  • Thanks :D It doesn't matter. We're here for helping ppl, not for the points. – Jonathan Feb 10 '12 at 09:11