获取WEBBROWSER滚动位置:莫名其妙的问题,大家帮看看

andy_123 2007-04-29 02:21:05
HRESULT hr;
IDispatch *pDisp = this->M_Browser.get_Document();
if(pDisp == NULL) return NULL;
// 获得Html文档指针
IHTMLDocument2 *pDocument = NULL;
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)pDocument );
if(!SUCCEEDED( hr ) || !pDocument) return NULL;
IHTMLElement *pBody = NULL;
hr = pDocument->get_body( &pBody );
if(!SUCCEEDED( hr ) || !pBody) return NULL;
pDocument->Release();
// 从body获得IHTMLElement2接口指针,用以访问滚动条
IHTMLElement2 *pElement = NULL;
hr = pBody->QueryInterface(IID_IHTMLElement2,(void**)&pElement);
if(!SUCCEEDED( hr ) || !pElement) return NULL;
pBody->Release();
long mScrollTop;
pElement->get_scrollTop(&mScrollTop);
pElement->Release();
return mScrollTop;


这样得到的滚动位置有时候是正确的,有时候总是0,请大家帮忙!!!
...全文
202 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
andy_123 2007-04-29
  • 打赏
  • 举报
回复
多谢,我还发现当网页中包含 !DOCTYPE 标记时,也无法取到网页的滚动位置,

真是搞不懂!!!
goodboyws 2007-04-29
  • 打赏
  • 举报
回复
这个是抓取文本,把你的代码放到
hr = pDoc2->get_body(&pBody);
if(SUCCEEDED(hr))
{
后面就可以
goodboyws 2007-04-29
  • 打赏
  • 举报
回复
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document(&spDoc.p);
IHTMLElementCollection* pCollection; IHTMLElementCollection* pCollection;
spDoc->get_all(&pCollection);
long len;
pCollection->get_length(&len);
for (long l=0; l<len; l++)
{
VARIANT varIndex, var2;
VariantInit(&varIndex);
VariantInit(&var2);
varIndex.vt = VT_I4;
varIndex.lVal = l;
IDispatch pDisp;
pCollection->item( varIndex, var2, &pDisp );
IHTMLElement* pElem;
pDisp->QueryInterface( IID_IHTMLElement, (LPVOID*) &pElem );
CComBSTR tagName;
pElement->get_tagName(&tagName);
CString str = tagName;
str.MakeUpper();
if (str == "FRAME" || str == "IFRAME")
{
HRESULT hr;
IHTMLWindow2 *pHTMLWindow;
IHTMLFrameBase2* pHTMLFrameBase2;
hr =pElement->QueryInterface(IID_IHTMLFrameBase2, (void**)&pHTMLFrameBase2);
pElement->Release();
hr = pHTMLFrameBase2->get_contentWindow(&pHTMLWindow);
pHTMLFrameBase2->Release();
IHTMLDocument2* pDoc2;
IHTMLElement* pBody;
hr = pHTMLWindow->get_document(&pDoc2);
hr = pDoc2->get_body(&pBody);
if(SUCCEEDED(hr))
{
BSTR bstrHTMLText;
hr = pBody->get_outerText(&bstrHTMLText);
CString strText = bstrHTMLText;
SysFreeString(bstrHTMLText);
pBody->Release();
}
}
pElem->Release();
}
pCollection->Release();
andy_123 2007-04-29
  • 打赏
  • 举报
回复
那么该如何解决这个问题呢?我只想得到最上层网页的滚动位置,请楼上的指教一下
goodboyws 2007-04-29
  • 打赏
  • 举报
回复
估计是内嵌Frame的问题

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧