检测 WebBrowser 控件上的滚动

发布于 2024-10-05 14:39:47 字数 1059 浏览 4 评论 0原文

我有以下代码,奇怪的是,它工作了几秒钟,然后停止工作(我的事件处理程序方法停止被调用):

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        webBrowser1.Navigate("google.com");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (!webBrowser1.IsBusy && webBrowser1.Url == e.Url && webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            HTMLWindowEvents_Event windowEvents = webBrowser1.Document.Window.DomWindow as HTMLWindowEvents_Event;
            windowEvents.onscroll += new HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
        }
    }

    private void windowEvents_onscroll()
    {
        HtmlDocument htmlDoc = webBrowser1.Document;
        int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
        string text = scrollTop.ToString();
    }
}

I have the following code that, weirdly, works for a couple of seconds and then stops working (my event handler method stops being called):

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        webBrowser1.Navigate("google.com");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (!webBrowser1.IsBusy && webBrowser1.Url == e.Url && webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            HTMLWindowEvents_Event windowEvents = webBrowser1.Document.Window.DomWindow as HTMLWindowEvents_Event;
            windowEvents.onscroll += new HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
        }
    }

    private void windowEvents_onscroll()
    {
        HtmlDocument htmlDoc = webBrowser1.Document;
        int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
        string text = scrollTop.ToString();
    }
}

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

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

发布评论

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

评论(1

两相知 2024-10-12 14:39:47

好的找到了解决方案:

    protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
    {
        Follow();
        if (!IsBusy && Url == e.Url && ReadyState == WebBrowserReadyState.Complete)
        {
            Document.Window.AttachEventHandler("onscroll", DocScroll);
        }
    }

如果以这种方式连接它就可以工作(到目前为止......)。甚至不需要使用 mshtml。

OK Found a solution:

    protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
    {
        Follow();
        if (!IsBusy && Url == e.Url && ReadyState == WebBrowserReadyState.Complete)
        {
            Document.Window.AttachEventHandler("onscroll", DocScroll);
        }
    }

If attached that way it works OK (so far...). Don't even need to use mshtml.

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