Scrolling WebBrowser Control

0.00 avg. rating (0% score) - 0 votes

This shows some ways to scroll a WebBrowser control pixel by pixel

Method 1 – Use JavaScript:

Me.WebBrowser1.Navigate(New Uri("javascript:window.scroll(100,100);"))

Where (100, 100) is the coordinate to scroll to.

This will work in a Windows Form application and probably in a .NET CF application if the device browser supports JavaScript.

Method 2 – By posting WM_HSCROLL/WM_VSCROLL message:

We must first find the webbrowser window handle. WebBrowser1.Handle alone is not sufficent as the control is an ActiveX object. The sequence of calls to FindWindowEx and the required parameters can be figured out by using Spy++:

Dim wbHandle As IntPtr = FindWindowEx(Me.WebBrowser1.Handle, IntPtr.Zero, "Shell Embedding", Nothing)
wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Shell DocObject View", Nothing)
wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Internet Explorer_Server", Nothing)

The following is necessary if the web browser does not have focus when the function is called.

Me.WebBrowser1.Document.Body.Focus()

Finally, send the message to scroll to the right.

SendMessage(wbHandle, WM_HSCROLL, SB_LINERIGHT, 0)

To scroll more precisely, e.g. pixelwise, use:

SendMessage(wbHandle, WM_VSCROLL, SB_THUMBPOSITION + &H10000 * nPos, Nothing)

Where nPos is the position to scroll to. Notice that SB_THUMBPOSITION + &H10000 * nPos creates an integer having nPos as the high WORD and SB_THUMBPOSITION as the low WORD.

Method 3 – By using GetScrollInfo and SetScrollInfo

SCROLLINFO si;
ZeroMemory(&si, sizeof(si));
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
::GetScrollInfo(this->webviewHandle, SB_VERT, &si);
si.nPos += 10;
::SetScrollInfo(this->webviewHandle, SB_VERT, &si, true);

This will scroll down 10 pixels. This works on other control but somehow does not work on the WebBrowser control as its scroll-bar is custom painted.

Reference:

http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=2727441&SiteID=1&pageid=0
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3951060&SiteID=1
http://www.tech-archive.net/Archive/InetSDK/microsoft.public.inetsdk.programming.webbrowser_ctl/2005-08/msg00092.html

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply