Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

c# - Create event handler for OnScroll for web browser control

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.

I would like to create an event something like this below? has any one done or seen this before?

 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...