剑痴乎

  • 首页
  • 文章分类
    • 音视频
    • WebRTC
    • 编程之美
    • Linux
    • Windows
    • 生活点滴
    • 校园生活
  • 参考
    • API参考
    • 实用工具
    • 测试音视频
    • 文档
  • 留言板
  • 关于
剑痴乎
代码为剑,如痴如醉
  1. 首页
  2. 编程之美
  3. 正文

MFC WebBrowser控件如何实现滚动条滑动

2015年7月21日 1916点热度 0人点赞 0条评论

最近在写一个自动刷流量的程序,用到了WebBrowser 控件,通过该控件往程序嵌入一个IE浏览器,MFC中如何嵌入该控件可以看下参考链接1。

由于要自动刷流量,所以就要模仿用户平时浏览网页的习惯,也就是打开网页,然后慢慢向下滚动鼠标。在程序中我是通过滑动右侧滚动条实现的。刚开始我的代码是参照链接3给的,不过该代码在有些网页下实现不了滚动条滑动,也就是没反应。我上网Google了下,发现好多人也遇到类似情况,比如链接2与链接4中的内容,不过都没人给出答案。我自己也试了好多种方法,比如修改WebBrowser的IE版本,最后也都是以失败告终。

过了几天在Google上搜索,无意间看到一段代码,试了下,有效果,在不同类型网页下都可以顺利滚动。哈哈哈。废话不多说,直接上代码:

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void XXXXDlg::autoScroll()
{
HRESULT hr;
 
//m_MyIE为我们的WebBrowser控件绑定的变量
IDispatch *pDisp = m_MyIE.get_Document();
 
// get document interface
IHTMLDocument2 *pDocument = NULL;
 
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDocument );
 
// take the body element from document...
IHTMLElement *pBody = NULL;
hr = pDocument->get_body( &pBody );
 
 
// from body we can get element2 interface,
// which allows us to do scrolling
IHTMLElement2 *pElement = NULL;
 
hr = pBody->QueryInterface(IID_IHTMLElement2,(void**)&pElement);
 
IHTMLDocument3 *pDocument3 = NULL;
IHTMLElement2 *pElement2 = NULL;
 
hr = pDisp->QueryInterface( IID_IHTMLDocument3, (void**)&pDocument3 );
 
IHTMLElement *docelmt = NULL;
hr = pDocument3->get_documentElement(&docelmt);
 
hr = docelmt->QueryInterface(IID_IHTMLElement2,(void**)&pElement2);
 
// now we are ready to scroll
long crvsrollpos,crvsrollpos2,scrolltop;
 
 
hr = pElement->get_scrollTop( &crvsrollpos );
hr = pElement2->get_scrollTop( &crvsrollpos2 );
scrolltop=crvsrollpos+crvsrollpos2;
 
// scroll down to 100th pixel from top
hr = pElement->put_scrollTop(100);
hr = pElement2->put_scrollTop(100);
}

通过上面函数的代码我们就可以实现WebBrowser向下滑动100像素。需要提醒的是,如果编译器提示undeclared identifier错误,我们需要包含mshtml.h文件。

1
#include <mshtml.h>

到此就大功告成了,用定时器结合该代码就可以实现一个自动刷流量的程序,详细的我就不说了。

参考链接:
1)VC中调用WebBrowser简单的实现过程(图解过程)
2)如何获得webBrowser控件滚动条滑块的位置问题补充 - VC/MFC / 界面
3)Programmatically scrolling WebBrowser control from Visual C/C++
4)webbrowser control: auto scroll + total webpage size

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可
标签: VC++
最后更新:2016年6月2日

Jeff

管理员——代码为剑,如痴如醉

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

相关文章
  • Google ProtoBuf协议介绍
  • Intel Media SDK 内存优化(转)
  • Visual Studio检查内存泄露方法
  • 网络字节转换到本地字节的函数模板
  • 如何让MFC或其他VS编译的程序支持高DPI

COPYRIGHT © 2025 jianchihu.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang