Click here to Skip to main content
Click here to Skip to main content

Programmatically scrolling WebBrowser control from Visual C/C++

, 22 Aug 2001
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] Pinmembercastielle_alana14-Oct-10 21:09 
Generalno shtypes.h file.... PinmemberAric Green24-Mar-10 18:59 
QuestionUsing this code with new versions of IE? PinmemberTrapper300129-Jan-10 8:33 
AnswerRe: Using this code with new versions of IE? [modified] PinmemberTrapper30012-Feb-10 5:39 
QuestionHow to do it in BCB6 PinmemberChriz_z26-Oct-06 1:57 
GeneralApplet in WebBrowser Pinmembercolin-12312-Sep-06 23:50 
GeneralThanks heaps. I spent hours looking for how to do that. Pinmemberjai20024-May-05 16:26 
GeneralThanks (works with CDHtmldialog as well) Pinmemberrichard sancenot4-Apr-05 5:08 
GeneralChanging the size of Scrollbar in WebBrowser control PinmemberWasif Ali4-Apr-05 1:10 
GeneralRe: Changing the size of Scrollbar in WebBrowser control Pinmemberrichard sancenot4-Apr-05 5:00 
GeneralUsing Scroll Pinmemberunidentify26-Mar-05 22:19 
Questionhow to seperate pictures? Pinmembersamathareddy_p28-Feb-05 6:48 
GeneralAny Help on how to do it with C# Pinmemberggmemo8-Feb-05 5:19 
GeneralRe: Any Help on how to do it with C# PinmemberBoyan N. Rabchev26-Mar-05 0:02 
GeneralRe: Any Help on how to do it with C# PinmemberpinkyNet13-Apr-07 10:38 
Generalhow to make webbrowser scroll with vb.net PinmemberJohannNutter6-Sep-09 17:50 
GeneralRe: Any Help on how to do it with C# PinmemberTheAlas23-Aug-06 14:31 
GeneralRe: Any Help on how to do it with C# PinmemberKnightrunner21-Sep-07 1:45 
GeneralWant IE automate and IpHelper Help Pinmembereiteit24-Nov-04 7:21 
QuestionCan we get the total scroll Height and width Pinmembereacho.woo13-Oct-04 17:31 
QuestionAllow ActiveX execution in browser? Pinmemberdragomir10-Sep-04 20:33 
GeneralCatching Scroll Bar Events PinmemberHumphrey Chakma6-Apr-04 19:45 
QuestionHow to get scroll bar position? Pinmemberw1424331-Mar-04 18:48 
AnswerRe: How to get scroll bar position? PinmemberHumphrey Chakma31-Mar-04 22:43 
GeneralA version that does not use IHTMLElement2 Pinmembermrxeng23-Feb-04 20: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 | Mobile
Web01 | 2.8.140821.2 | Last Updated 23 Aug 2001
Article Copyright 2001 by Valters Vingolds
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid