|
发表于 2020-8-31 20:00:01
|
显示全部楼层
RESULT hr;
IDispatch *pDisp = m_cMyWebBrowser.GetDocument();
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_IHTMLElement,(void**)&pElement);
ASSERT(SUCCEEDED(hr));
ASSERT( pElement );
IHTMLTextContainer *pCont;
pBody->QueryInterface(IID_IHTMLTextContainer, (LPVOID *)(&pCont));
ASSERT(pCont);
// 向下滚动100个像素
// pElement->put_scrollTop( 100 );
// 获得滚动条高度
long scroll_height;
pCont->get_scrollHeight( &scroll_height );
// 获得滚动条宽度
long scroll_width;
if(FAILED(pCont->get_scrollWidth( &scroll_width )))
AfxMessageBox("get scrollWidth failed.");
// 获得滚动条位置,从顶端开始
long scroll_top;
if(FAILED(pCont->get_scrollTop( &scroll_top )))
AfxMessageBox("get scrollWidth failed.");
// 获得滚动条位置,从左端开始
long scroll_left;
if(FAILED(pCont->get_scrollLeft( &scroll_left )))
AfxMessageBox("get scrollWidth failed.");
CString str;
str.Format("%d,%d,%d,%d",scroll_height,scroll_width,scroll_top,scroll_left);
AfxMessageBox(str);
pCont->Release();
pElement->Release();
pBody->Release();
pDocument->Release();
pDisp->Release();
Ps:如果页面中用到了
WEB标准会使ScrollTop,ScrollLeft属性失效!!! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
|
|