Click here to Skip to main content
15,881,248 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 254.5K   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

 
GeneralRe: A version that does not use IHTMLElement2 Pin
Jo Fredrickson16-Mar-04 17:46
Jo Fredrickson16-Mar-04 17:46 
GeneralRe: A version that does not use IHTMLElement2 Pin
techratna13-Apr-09 6:47
techratna13-Apr-09 6:47 
GeneralRe: A version that does not use IHTMLElement2 Pin
Trapper300128-Jan-10 20:20
Trapper300128-Jan-10 20:20 
GeneralRe: A version that does not use IHTMLElement2 Pin
Aric Wang24-Mar-10 18:46
Aric Wang24-Mar-10 18:46 
QuestionHiding scrollbar? Pin
xxhimanshu30-Dec-03 20:26
xxhimanshu30-Dec-03 20:26 
GeneralGreat Code Does Yahoo Messenger Uses Code like this for Main Chat window Pin
Vikrant Vikrant21-Sep-03 2:05
Vikrant Vikrant21-Sep-03 2:05 
QuestionThe code work fine for the scrollbar but can i hide the scrollbar?? Pin
rbouraoui20-Jun-03 6:05
rbouraoui20-Jun-03 6:05 
AnswerRe: The code work fine for the scrollbar but can i hide the scrollbar?? Pin
wireman9-Jul-03 15:49
wireman9-Jul-03 15:49 
GeneralRe: The code work fine for the scrollbar but can i hide the scrollbar?? Pin
markpeng24-Nov-08 16:14
markpeng24-Nov-08 16:14 
Questionhow to bypass intermediate response pages when using WebBrowser controls Pin
shivsun15-Jun-03 19:33
shivsun15-Jun-03 19:33 
GeneralAnother Programmatically scrolling WebBrowser control from Visual C/C++ Pin
19-Mar-02 20:59
suss19-Mar-02 20:59 
GeneralRe: Another Programmatically scrolling WebBrowser control from Visual C/C++ Pin
benben22-Jan-03 4:30
benben22-Jan-03 4:30 
GeneralRe: Another Programmatically scrolling WebBrowser control from Visual C/C++ Pin
jeremysay24-Jan-03 21:32
jeremysay24-Jan-03 21:32 
GeneralRe: Another Programmatically scrolling WebBrowser control from Visual C/C++ Pin
sanskypotov1-Sep-04 23:43
sanskypotov1-Sep-04 23:43 
GeneralHelp finding IID_IHTMLElement2 definition Pin
Michael Kennedy29-Jan-02 8:41
Michael Kennedy29-Jan-02 8:41 
GeneralI was badly needing this code. but stillI i am unable to find IHTMLElemet2 on my system Pin
Umar Riaz27-Sep-01 11:04
Umar Riaz27-Sep-01 11:04 
GeneralRe: I was badly needing this code. but stillI i am unable to find IHTMLElemet2 on my system Pin
27-Sep-01 12:19
suss27-Sep-01 12:19 
GeneralIHTMLElement2 not found Pin
Harold Bamford20-Sep-01 13:36
Harold Bamford20-Sep-01 13:36 
GeneralRe: IHTMLElement2 not found Pin
21-Sep-01 11:43
suss21-Sep-01 11:43 
GeneralRe: IHTMLElement2 not found Pin
Harold Bamford21-Sep-01 11:57
Harold Bamford21-Sep-01 11:57 

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.