Click here to Skip to main content
6,595,444 members and growing! (20,188 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » HTML Browser Controls     Intermediate

Programmatically scrolling WebBrowser control from Visual C/C++

By Valters Vingolds

Article describes how to obtain IHTML interfaces to prrogrammatically scroll WebBrowser control from Visual C/C++.
VC6Win2K, Visual Studio, MFC, Dev
Posted:22 Aug 2001
Views:118,066
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 6.00 Rating: 4.54 out of 5

1
1 vote, 6.3%
2
2 votes, 12.5%
3

4
13 votes, 81.3%
5

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

About the Author

Valters Vingolds


Member

Location: Latvia Latvia

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
GeneralHow to do it in BCB6 PinmemberChriz_z2:57 26 Oct '06  
GeneralApplet in WebBrowser Pinmembercolin-1230:50 13 Sep '06  
GeneralThanks heaps. I spent hours looking for how to do that. Pinmemberjai200217:26 4 May '05  
GeneralThanks (works with CDHtmldialog as well) Pinmemberrichard sancenot6:08 4 Apr '05  
GeneralChanging the size of Scrollbar in WebBrowser control PinmemberWasif Ali2:10 4 Apr '05  
GeneralRe: Changing the size of Scrollbar in WebBrowser control Pinmemberrichard sancenot6:00 4 Apr '05  
GeneralUsing Scroll Pinmemberunidentify23:19 26 Mar '05  
Generalhow to seperate pictures? Pinmembersamathareddy_p7:48 28 Feb '05  
GeneralAny Help on how to do it with C# Pinmemberggmemo6:19 8 Feb '05  
GeneralRe: Any Help on how to do it with C# PinmemberBoyan N. Rabchev1:02 26 Mar '05  
GeneralRe: Any Help on how to do it with C# PinmemberpinkyNet11:38 13 Apr '07  
Generalhow to make webbrowser scroll with vb.net PinmemberJohannNutter18:50 6 Sep '09  
GeneralRe: Any Help on how to do it with C# PinmemberTheAlas15:31 23 Aug '06  
GeneralRe: Any Help on how to do it with C# PinmemberKnightrunner2:45 21 Sep '07  
GeneralWant IE automate and IpHelper Help Pinmembereiteit8:21 24 Nov '04  
GeneralCan we get the total scroll Height and width Pinmembereacho.woo18:31 13 Oct '04  
GeneralAllow ActiveX execution in browser? Pinmemberdragomir21:33 10 Sep '04  
GeneralCatching Scroll Bar Events PinmemberHumphrey Chakma20:45 6 Apr '04  
GeneralHow to get scroll bar position? Pinmemberw1424319:48 31 Mar '04  
GeneralRe: How to get scroll bar position? PinmemberHumphrey Chakma23:43 31 Mar '04  
GeneralA version that does not use IHTMLElement2 Pinmembermrxeng21:45 23 Feb '04  
GeneralRe: A version that does not use IHTMLElement2 PinmemberJo Fredrickson18:46 16 Mar '04  
GeneralRe: A version that does not use IHTMLElement2 Pinmembertechratna7:47 13 Apr '09  
GeneralHiding scrollbar? Pinmemberxxhimanshu21:26 30 Dec '03  
GeneralGreat Code Does Yahoo Messenger Uses Code like this for Main Chat window PinmemberVikrant Vikrant3:05 21 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Aug 2001
Editor: Smitha Vijayan
Copyright 2001 by Valters Vingolds
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project