如何查找 Web 浏览器控件中元素的位置?

发布于 2025-01-03 18:03:44 字数 427 浏览 3 评论 0原文

我有一个带有网络浏览器控件的表单,它加载一个网页(它工作正常,页面加载正常)

现在我的问题是,我想查找特定的 url 链接是在折叠下方还是在折叠上方 (我的意思是,用户是否必须向下滚动才能看到此链接) 这个 v 是否在不滚动的情况下可见,或者我们需要滚动才能看到它。我希望我很清楚

我已经进行了广泛的搜索,但看起来没有关于查找 html 元素位置(当前视图上方或下方)的

信息有人知道这件事并能指出我正确的方向吗? (我正在寻找 C# 解决方案 - WinForms)

更新:非常感谢 John Koerner 提供的代码。非常感谢他为解决我的问题所付出的时间和精力。

还有乔纳森和其他人也......我希望我也可以将乔纳森的回复标记为答案,但它只允许将一个回复标记为答案。他的评论也是清晰而有用的暗示。谢谢你们太棒了!!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2025-01-10 18:03:44

好的,我已经在 google 和 stackoverflow 上对此进行了测试,它似乎有效:

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

然后要使用它,您只需调用:

isElementVisible(webBrowser1, "topbar")  //StackOverflow's top navigation bar

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
浅笑轻吟梦一曲 2025-01-10 18:03:44

我有一个可行的想法(从未尝试过,但这是我能为您提供的最好的,抱歉)

您可以在 webbrowsercontrol 中调用 javascript 函数: LINK

您还可以制作 JavaScript 函数给你一个元素的位置: LINK

如果你混合这两个概念,你可以知道该元素是否可见,就像您知道 webbrowsercontrol 的大小一样。

请注意,您可以将 javascript 代码注入到 webbrowsercontrol。这篇文章解释了如何做到这一点:LINK

祝你好运。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文