Click here to Skip to main content
11,481,265 members (62,681 online)
Click here to Skip to main content

Programmatically scrolling WebBrowser control from Visual C/C++

, 22 Aug 2001 179.2K 46
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

No Biography provided

Comments and Discussions

 
Generalie7 crash for scrolling [modified] Pin
castielle_alana14-Oct-10 22:09
membercastielle_alana14-Oct-10 22:09 
Generalno shtypes.h file.... Pin
Aric Green24-Mar-10 19:59
memberAric Green24-Mar-10 19:59 
QuestionUsing this code with new versions of IE? Pin
Trapper300129-Jan-10 9:33
memberTrapper300129-Jan-10 9:33 
AnswerRe: Using this code with new versions of IE? [modified] Pin
Trapper30012-Feb-10 6:39
memberTrapper30012-Feb-10 6:39 
QuestionHow to do it in BCB6 Pin
Chriz_z26-Oct-06 2:57
memberChriz_z26-Oct-06 2:57 
GeneralApplet in WebBrowser Pin
colin-12313-Sep-06 0:50
membercolin-12313-Sep-06 0:50 
GeneralThanks heaps. I spent hours looking for how to do that. Pin
jai20024-May-05 17:26
memberjai20024-May-05 17:26 
GeneralThanks (works with CDHtmldialog as well) Pin
richard sancenot4-Apr-05 6:08
memberrichard sancenot4-Apr-05 6:08 
GeneralChanging the size of Scrollbar in WebBrowser control Pin
Wasif Ali4-Apr-05 2:10
memberWasif Ali4-Apr-05 2:10 
GeneralRe: Changing the size of Scrollbar in WebBrowser control Pin
richard sancenot4-Apr-05 6:00
memberrichard sancenot4-Apr-05 6:00 
GeneralUsing Scroll Pin
unidentify26-Mar-05 23:19
memberunidentify26-Mar-05 23:19 
Questionhow to seperate pictures? Pin
samathareddy_p28-Feb-05 7:48
membersamathareddy_p28-Feb-05 7:48 
GeneralAny Help on how to do it with C# Pin
ggmemo8-Feb-05 6:19
memberggmemo8-Feb-05 6:19 
GeneralRe: Any Help on how to do it with C# Pin
Boyan N. Rabchev26-Mar-05 1:02
memberBoyan N. Rabchev26-Mar-05 1:02 
GeneralRe: Any Help on how to do it with C# Pin
pinkyNet13-Apr-07 11:38
memberpinkyNet13-Apr-07 11:38 
Generalhow to make webbrowser scroll with vb.net Pin
JohannNutter6-Sep-09 18:50
memberJohannNutter6-Sep-09 18:50 
GeneralRe: Any Help on how to do it with C# Pin
TheAlas23-Aug-06 15:31
memberTheAlas23-Aug-06 15:31 
GeneralRe: Any Help on how to do it with C# Pin
Knightrunner21-Sep-07 2:45
memberKnightrunner21-Sep-07 2:45 
GeneralWant IE automate and IpHelper Help Pin
eiteit24-Nov-04 8:21
membereiteit24-Nov-04 8:21 
QuestionCan we get the total scroll Height and width Pin
eacho.woo13-Oct-04 18:31
membereacho.woo13-Oct-04 18:31 
QuestionAllow ActiveX execution in browser? Pin
dragomir10-Sep-04 21:33
memberdragomir10-Sep-04 21:33 
GeneralCatching Scroll Bar Events Pin
Humphrey Chakma6-Apr-04 20:45
memberHumphrey Chakma6-Apr-04 20:45 
QuestionHow to get scroll bar position? Pin
w1424331-Mar-04 19:48
memberw1424331-Mar-04 19:48 
AnswerRe: How to get scroll bar position? Pin
Humphrey Chakma31-Mar-04 23:43
memberHumphrey Chakma31-Mar-04 23:43 
GeneralA version that does not use IHTMLElement2 Pin
mrxeng23-Feb-04 21:45
membermrxeng23-Feb-04 21:45 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 23 Aug 2001
Article Copyright 2001 by Valters Vingolds
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid