Click here to Skip to main content
15,355,133 members
Articles / Desktop Programming / MFC
Posted 22 Aug 2001


48 bookmarked

Programmatically scrolling WebBrowser control from Visual C/C++

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
22 Aug 2001
Article describes how to obtain IHTML interfaces to prrogrammatically scroll WebBrowser control from Visual C/C++.


For some time I struggled to do these simple tasks in Visual C++:

  1. How to get browser window to scroll programmatically?

    It does not accept Windows scroll messages. Calling the MFC GetScrollInfo APIs does not do anything. But there is a way.

  2. To scroll we use IHTMLElement2 API get_scrollTop/put_scrollTop... but how do I obtain IHTMLElement2?

The way is a bit obscure...

Now, the code

// All this code does is what
// "m_browser.Document.Body.ScrollTop = 100;"
// does in VB. Gotta love COM in C++.

// let's say m_browser is the WebBrowser's member variable.


// get the document dispatch from browser
IDispatch *pDisp = m_browser.GetDocument();
ASSERT( pDisp ); //if NULL, we failed

// get document interface
IHTMLDocument2 *pDocument = NULL;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDocument );
ASSERT( pDocument );

// this is the trick!
// take the body element from document...
IHTMLElement *pBody = NULL;
hr = pDocument->get_body( &pBody );
ASSERT( pBody );

// from body we can get element2 interface,
// which allows us to do scrolling
IHTMLElement2 *pElement = NULL;
hr = pBody->QueryInterface(IID_IHTMLElement2,(void**)&pElement);
ASSERT( pElement );

// now we are ready to scroll
// scroll down to 100th pixel from top
pElement->put_scrollTop( 100 );

// try to get the whole page size - but the returned number
// is not allways correct. especially with pages that use dynamic html
// tricks...
long scroll_height;
pElement->get_scrollHeight( &s );

// we can use this workaround!
long real_scroll_height;
pElement->put_scrollTop( 20000000 ); // ask to scroll really far down...
pElement->get_scrollTop( &real_scroll_height );
real_scroll_height += window_height; // will return the scroll height
// for the first visible pixel, to get whole html page size must
// add the window's height... (to obtain window_height is
// left as an exercise for the reader)

// print to debug output
TRACE( "real scroll height: %ld, get_scrollHeight: %ld\n",
                 real_scroll_height, scroll_height );

And there we have it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

Comments and Discussions

Generalie7 crash for scrolling [modified] Pin
castielle_alana14-Oct-10 21:09
Membercastielle_alana14-Oct-10 21:09 
Generalno shtypes.h file.... Pin
Aric Wang24-Mar-10 18:59
MemberAric Wang24-Mar-10 18:59 
QuestionUsing this code with new versions of IE? Pin
Trapper300129-Jan-10 8:33
MemberTrapper300129-Jan-10 8:33 
AnswerRe: Using this code with new versions of IE? [modified] Pin
Trapper30012-Feb-10 5:39
MemberTrapper30012-Feb-10 5:39 
QuestionHow to do it in BCB6 Pin
Chriz_z26-Oct-06 1:57
MemberChriz_z26-Oct-06 1:57 
GeneralApplet in WebBrowser Pin
colin-12312-Sep-06 23:50
Membercolin-12312-Sep-06 23:50 
GeneralThanks heaps. I spent hours looking for how to do that. Pin
jai20024-May-05 16:26
Memberjai20024-May-05 16:26 
GeneralThanks (works with CDHtmldialog as well) Pin
richard sancenot4-Apr-05 5:08
Memberrichard sancenot4-Apr-05 5:08 
GeneralChanging the size of Scrollbar in WebBrowser control Pin
Wasif Ali4-Apr-05 1:10
MemberWasif Ali4-Apr-05 1:10 
GeneralRe: Changing the size of Scrollbar in WebBrowser control Pin
richard sancenot4-Apr-05 5:00
Memberrichard sancenot4-Apr-05 5:00 
GeneralUsing Scroll Pin
unidentify26-Mar-05 22:19
Memberunidentify26-Mar-05 22:19 
Questionhow to seperate pictures? Pin
Member 46991628-Feb-05 6:48
MemberMember 46991628-Feb-05 6:48 
GeneralAny Help on how to do it with C# Pin
ggmemo8-Feb-05 5:19
Memberggmemo8-Feb-05 5:19 
GeneralRe: Any Help on how to do it with C# Pin
Boyan N. Rabchev26-Mar-05 0:02
MemberBoyan N. Rabchev26-Mar-05 0:02 
GeneralRe: Any Help on how to do it with C# Pin
pinkyNet13-Apr-07 10:38
MemberpinkyNet13-Apr-07 10:38 
Generalhow to make webbrowser scroll with Pin
JohannNutter6-Sep-09 17:50
MemberJohannNutter6-Sep-09 17:50 
GeneralRe: Any Help on how to do it with C# Pin
Bojan Sala23-Aug-06 14:31
professionalBojan Sala23-Aug-06 14:31 
GeneralRe: Any Help on how to do it with C# Pin
Knightrunner21-Sep-07 1:45
MemberKnightrunner21-Sep-07 1:45 
GeneralWant IE automate and IpHelper Help Pin
eiteit24-Nov-04 7:21
Membereiteit24-Nov-04 7:21 
QuestionCan we get the total scroll Height and width Pin
eacho.woo13-Oct-04 17:31
Membereacho.woo13-Oct-04 17:31 
QuestionAllow ActiveX execution in browser? Pin
dragomir10-Sep-04 20:33
Memberdragomir10-Sep-04 20:33 
GeneralCatching Scroll Bar Events Pin
Humphrey Chakma6-Apr-04 19:45
MemberHumphrey Chakma6-Apr-04 19:45 
QuestionHow to get scroll bar position? Pin
w1424331-Mar-04 18:48
Memberw1424331-Mar-04 18:48 
AnswerRe: How to get scroll bar position? Pin
Humphrey Chakma31-Mar-04 22:43
MemberHumphrey Chakma31-Mar-04 22:43 
GeneralA version that does not use IHTMLElement2 Pin
mrxeng23-Feb-04 20:45
Membermrxeng23-Feb-04 20:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.