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
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.
The real event name is "onscroll" not "OnScroll".
MSDN:http://msdn.microsoft.com/en-us/library/ie/ms536966(v=vs.85).aspx
Following code is firing the method when event occured.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler);
}
public void OnScrollEventHandler(object sender, EventArgs e)
{
}