Click here to Skip to main content
Licence 
First Posted 22 Aug 2001
Views 155,849
Bookmarked 42 times

Programmatically scrolling WebBrowser control from Visual C/C++

By | 22 Aug 2001 | Article
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

About the Author

Valters Vingolds



Latvia Latvia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalie7 crash for scrolling [modified] Pinmembercastielle_alana21:09 14 Oct '10  
Generalno shtypes.h file.... PinmemberAric Green18:59 24 Mar '10  
QuestionUsing this code with new versions of IE? PinmemberTrapper30018:33 29 Jan '10  
AnswerRe: Using this code with new versions of IE? [modified] PinmemberTrapper30015:39 2 Feb '10  
QuestionHow to do it in BCB6 PinmemberChriz_z1:57 26 Oct '06  
GeneralApplet in WebBrowser Pinmembercolin-12323:50 12 Sep '06  
GeneralThanks heaps. I spent hours looking for how to do that. Pinmemberjai200216:26 4 May '05  
GeneralThanks (works with CDHtmldialog as well) Pinmemberrichard sancenot5:08 4 Apr '05  
GeneralChanging the size of Scrollbar in WebBrowser control PinmemberWasif Ali1:10 4 Apr '05  
GeneralRe: Changing the size of Scrollbar in WebBrowser control Pinmemberrichard sancenot5:00 4 Apr '05  
GeneralUsing Scroll Pinmemberunidentify22:19 26 Mar '05  
Questionhow to seperate pictures? Pinmembersamathareddy_p6:48 28 Feb '05  
GeneralAny Help on how to do it with C# Pinmemberggmemo5:19 8 Feb '05  
GeneralRe: Any Help on how to do it with C# PinmemberBoyan N. Rabchev0:02 26 Mar '05  
GeneralRe: Any Help on how to do it with C# PinmemberpinkyNet10:38 13 Apr '07  
Generalhow to make webbrowser scroll with vb.net PinmemberJohannNutter17:50 6 Sep '09  
GeneralRe: Any Help on how to do it with C# PinmemberTheAlas14:31 23 Aug '06  
GeneralRe: Any Help on how to do it with C# PinmemberKnightrunner1:45 21 Sep '07  
This is how you do it in C# using VS 2005:
 
Add a COM reference in your project to mshtml (shows up as "Microsoft HTML Object Library" in the Add Reference dialog).
Add the following to your buttonDown Clicked handler:
 
private void buttonDown_Click(object sender, EventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
if (doc == null)
return;
mshtml.IHTMLDocument2 htmlDoc = (mshtml.IHTMLDocument2)doc.DomDocument;
htmlDoc.parentWindow.scrollBy(0, 100);
}

GeneralWant IE automate and IpHelper Help Pinmembereiteit7:21 24 Nov '04  
QuestionCan we get the total scroll Height and width Pinmembereacho.woo17:31 13 Oct '04  
QuestionAllow ActiveX execution in browser? Pinmemberdragomir20:33 10 Sep '04  
GeneralCatching Scroll Bar Events PinmemberHumphrey Chakma19:45 6 Apr '04  
QuestionHow to get scroll bar position? Pinmemberw1424318:48 31 Mar '04  
AnswerRe: How to get scroll bar position? PinmemberHumphrey Chakma22:43 31 Mar '04  
GeneralA version that does not use IHTMLElement2 Pinmembermrxeng20:45 23 Feb '04  

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.

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