WebBrowser的滚动条不是一般的Windows滚动条,用GetScrollPos或GetScrollInfo等API是不能访问的。下面的代码演示了在VC中如何通过HTML接口来访问浏览器的滚动条。
HRESULT hr;
IDispatch *pDisp = GetHtmlDocument();
ASSERT( pDisp ); //if NULL, we failed
// 获得Html文档指针
IHTMLDocument2 *pDocument = NULL;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDocument );
ASSERT( SUCCEEDED( hr ) );
ASSERT( pDocument );
HRESULT hr;
IDispatch *pDisp = GetHtmlDocument();
ASSERT( pDisp ); //if NULL, we failed
// 获得Html文档指针
IHTMLDocument2 *pDocument = NULL;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDocument );
ASSERT( SUCCEEDED( hr ) );
ASSERT( pDocument );
IHTMLElement *pBody = NULL;
hr = pDocument->get_body( &pBody );
ASSERT( SUCCEEDED( hr ) );
ASSERT( pBody );
// 从body获得IHTMLElement2接口指针,用以访问滚动条
IHTMLElement2 *pElement = NULL;
hr = pBody->QueryInterface(IID_IHTMLElement2,(void**)&pElement);
ASSERT(SUCCEEDED(hr));
ASSERT( pElement );
// 向下滚动100个像素
pElement->put_scrollTop( 100 );
// 获得滚动条高度
long scroll_height;
pElement->get_scrollHeight( &scroll_height );
// 获得滚动条宽度
long scroll_height;
pElement->get_scrollWidth( &scroll_width );
// 获得滚动条位置,从顶端开始
long scroll_top;
pElement->get_scrollTop( &scroll_top );
(转载)