JCHub

  • Home
  • Category
    • A/V
    • WebRTC
    • Beauty of Programming
    • Linux
    • Windows
    • Moments of Life
    • Campus Life
  • Reference
    • API Reference
    • Utilities
    • AV Test
    • Doc
  • Message Board
  • About
JCHub
Code as My Sword, Lost in Obsession
  1. Main page
  2. Beauty of Programming
  3. Main content

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

2015年7月21日 2156hotness 0likes 0comments

最近在写一个自动刷流量的程序,用到了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

This article is licensed with Creative Commons Attribution-NonCommercial-No Derivatives 4.0 International License
Tag: VC++
Last updated:2016年6月2日

Jeff

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

Tip the author Like
< Last article
Next article >

Comments

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts
  • Nodejs C++ addon开发简介
  • Google ProtoBuf协议介绍
  • Intel Media SDK 内存优化(转)
  • Visual Studio检查内存泄露方法
  • 网络字节转换到本地字节的函数模板
Categories

COPYRIGHT © 2026 jianchihu.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang