为 Web 浏览器控件创建 OnScroll 事件处理程序 [英] Create event handler for OnScroll for web browser control

查看:24
本文介绍了为 Web 浏览器控件创建 OnScroll 事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人成功地将鼠标滚动事件捕获到网络浏览器组件中?

Has any one successfully trapped the event of mouse scroll in a web browerser component?

我想同时滚动两个网络浏览器控件.

I have two web browser controls i would like to scroll at the same time.

但是网络浏览器没有滚动事件.

But there are no scroll events for web browsers.

我想在下面创建一个类似这样的活动?有人做过或见过吗?

 private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
                Document.Window.AttachEventHandler("OnScroll");            
        }

在这里我会调用我的事件并继续执行代码.

Here i would call my event and proceed with the code.

private void windowEvents_OnScroll()
{
int nPos = GetScrollPos(webCompareSQL.Handle, (int)ScrollBarType.SbVert); 
nPos <<= 16;
uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos;
SendMessage(WebPrevSQL.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0));        
}

我找到了这段代码,但不知道如何使用它.这是一个事件.

I have found this code but don't know how to use it. its an event.

webCompareSQL.Document.Window.Scroll

推荐答案

我能够让这个工作如下.此示例假定两个 Web 浏览器控件都导航到同一个 Url.除了垂直滚动条之外,我还同步水平滚动条 - 如果不需要,可以省略.

I was able to get this working as follows. This example assumes that both web browser controls are navigating to the same Url. I am also syncing the horizontal scrollbar in addition to the vertical - this can be omitted if it is not required.

webBrowser1.DocumentCompleted
    += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser2.DocumentCompleted
    += new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);

NavigateToPage("www.google.com");

....

private void NavigateToPage(string url)
{
    webBrowser1.Navigate(url);
    webBrowser2.Navigate(url);
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler1);
}

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser2.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler2);
}

public void OnScrollEventHandler1(object sender, EventArgs e)
{           
    webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollTop
        = webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop;
    webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollLeft
        = webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
}

public void OnScrollEventHandler2(object sender, EventArgs e)
{
    webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop
        = webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollTop;
    webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollLeft
        = webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
} 

我注意到您在 中的评论.NET中如何获取webbrowser控件的滚动条位置,与此操作相关

I note your comment in How to retrieve the scrollbar position of the webbrowser control in .NET, relating to this operation

webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop

不工作.我可以确认这绝对适用于我的机器,因此如果此代码在您的机器上不起作用,我可以寻找替代方案.

not working. I can confirm that this definitely works on my machine, so if this code does not work on yours I can look into alternatives.

这篇关于为 Web 浏览器控件创建 OnScroll 事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆