Click here to Skip to main content
16,020,512 members
Articles / Desktop Programming / MFC
Article

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 260.6K   48   45
Article describes how to obtain IHTML interfaces to prrogrammatically scroll WebBrowser control from Visual C/C++.

Introduction

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.

HRESULT hr;

// 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( SUCCEEDED( hr ) );
ASSERT( pDocument );

//
// this is the trick!
// take the body element from document...
//
IHTMLElement *pBody = NULL;
hr = pDocument->get_body( &pBody );
ASSERT( SUCCEEDED( hr ) );
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(SUCCEEDED(hr));
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.

License

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


Written By
Latvia Latvia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalie7 crash for scrolling [modified] Pin
castielle_alana14-Oct-10 21:09
castielle_alana14-Oct-10 21:09 
Generalno shtypes.h file.... Pin
Aric Wang24-Mar-10 18:59
Aric Wang24-Mar-10 18:59 
QuestionUsing this code with new versions of IE? Pin
Trapper300129-Jan-10 8:33
Trapper300129-Jan-10 8:33 
AnswerRe: Using this code with new versions of IE? [modified] Pin
Trapper30012-Feb-10 5:39
Trapper30012-Feb-10 5:39 
QuestionHow to do it in BCB6 Pin
Chriz_z26-Oct-06 1:57
Chriz_z26-Oct-06 1:57 
GeneralApplet in WebBrowser Pin
colin-12312-Sep-06 23:50
colin-12312-Sep-06 23:50 
GeneralThanks heaps. I spent hours looking for how to do that. Pin
jai20024-May-05 16:26
jai20024-May-05 16:26 
GeneralThanks (works with CDHtmldialog as well) Pin
richard sancenot4-Apr-05 5:08
richard sancenot4-Apr-05 5:08 
GeneralChanging the size of Scrollbar in WebBrowser control Pin
Wasif Ali4-Apr-05 1:10
Wasif Ali4-Apr-05 1:10 
GeneralRe: Changing the size of Scrollbar in WebBrowser control Pin
richard sancenot4-Apr-05 5:00
richard sancenot4-Apr-05 5:00 
GeneralUsing Scroll Pin
unidentify26-Mar-05 22:19
unidentify26-Mar-05 22:19 
Questionhow to seperate pictures? Pin
Member 46991628-Feb-05 6:48
Member 46991628-Feb-05 6:48 
GeneralAny Help on how to do it with C# Pin
ggmemo8-Feb-05 5:19
ggmemo8-Feb-05 5:19 
GeneralRe: Any Help on how to do it with C# Pin
Boyan N. Rabchev26-Mar-05 0:02
Boyan N. Rabchev26-Mar-05 0:02 
GeneralRe: Any Help on how to do it with C# Pin
pinkyNet13-Apr-07 10:38
pinkyNet13-Apr-07 10:38 
Generalhow to make webbrowser scroll with vb.net Pin
JohannNutter6-Sep-09 17:50
JohannNutter6-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
Knightrunner21-Sep-07 1:45 
GeneralWant IE automate and IpHelper Help Pin
eiteit24-Nov-04 7:21
eiteit24-Nov-04 7:21 
QuestionCan we get the total scroll Height and width Pin
eacho.woo13-Oct-04 17:31
eacho.woo13-Oct-04 17:31 
Thank for your good work.It works well.
But now i have got a problem. can we get the whole height & width the scroll bar will scroll?
Does anybody could help me out?? thank you

QuestionAllow ActiveX execution in browser? Pin
dragomir10-Sep-04 20:33
dragomir10-Sep-04 20:33 
GeneralCatching Scroll Bar Events Pin
Humphrey Chakma6-Apr-04 19:45
Humphrey Chakma6-Apr-04 19:45 
QuestionHow to get scroll bar position? Pin
w1424331-Mar-04 18:48
w1424331-Mar-04 18:48 
AnswerRe: How to get scroll bar position? Pin
Humphrey Chakma31-Mar-04 22:43
Humphrey Chakma31-Mar-04 22:43 
GeneralA version that does not use IHTMLElement2 Pin
mrxeng23-Feb-04 20:45
mrxeng23-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.