从IE浏览器获取当前页面内容可能有多种方式,今天我所介绍的是其中一种方法。基本原理:当鼠标点击当前IE页面时,获取鼠标的坐标位置,根据鼠标位置获取当前页面的句柄,然后根据句柄,调用win32的东西进而获取页面内容。具体代码:

  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3. lock (currentLock)
  4. {
  5. System.Drawing.Point MousePoint = System.Windows.Forms.Form.MousePosition;
  6. if (_leftClick)
  7. {
  8. timer1.Stop();
  9. _leftClick = false;
  10.  
  11. _lastDocument = GetHTMLDocumentFormHwnd(GetPointControl(MousePoint, false));
  12. if (_lastDocument != null)
  13. {
  14. if (_getDocument)
  15. {
  16. _getDocument = true;
  17. try
  18. {
  19. string url = _lastDocument.url;
  20. string html = _lastDocument.documentElement.outerHTML;
  21. string cookie = _lastDocument.cookie;
  22. string domain = _lastDocument.domain;
  23.  
  24. var resolveParams = new ResolveParam
  25. {
  26. Url = new Uri(url),
  27. Html = html,
  28. PageCookie = cookie,
  29. Domain = domain
  30. };
  31.  
  32. RequetResove(resolveParams);
  33. }
  34. catch (Exception ex)
  35. {
  36. System.Windows.MessageBox.Show(ex.Message);
  37. Console.WriteLine(ex.Message);
  38. Console.WriteLine(ex.StackTrace);
  39. }
  40. }
  41. }
  42. else
  43. {
  44. new MessageTip().Show("xx", "当前页面不是IE浏览器页面,或使用了非IE内核浏览器,如火狐,搜狗等。请使用IE浏览器打开网页");
  45. }
  46.  
  47. _getDocument = false;
  48. }
  49. else
  50. {
  51. _pointFrm.Left = MousePoint.X + ;
  52. _pointFrm.Top = MousePoint.Y + ;
  53. }
  54. }
  55.  
  56. }

第11行的  GetHTMLDocumentFormHwnd(GetPointControl(MousePoint, false))  分解下,先从鼠标坐标获取页面的句柄:

  1. public static IntPtr GetPointControl(System.Drawing.Point p, bool allControl)
  2. {
  3. IntPtr handle = Win32APIsFull.WindowFromPoint(p);
  4. if (handle != IntPtr.Zero)
  5. {
  6. System.Drawing.Rectangle rect = default(System.Drawing.Rectangle);
  7. if (Win32APIsFull.GetWindowRect(handle, out rect))
  8. {
  9. return Win32APIsFull.ChildWindowFromPointEx(handle, new System.Drawing.Point(p.X - rect.X, p.Y - rect.Y), allControl ? Win32APIsFull.CWP.ALL : Win32APIsFull.CWP.SKIPINVISIBLE);
  10. }
  11. }
  12. return IntPtr.Zero;
  13.  
  14. }

接下来,根据句柄获取页面内容:

  1. public static HTMLDocument GetHTMLDocumentFormHwnd(IntPtr hwnd)
  2. {
  3. IntPtr result = Marshal.AllocHGlobal();
  4. Object obj = null;
  5.  
  6. Console.WriteLine(Win32APIsFull.SendMessageTimeoutA(hwnd, HTML_GETOBJECT_mid, , , , , result));
  7. if (Marshal.ReadInt32(result) != )
  8. {
  9. Console.WriteLine(Win32APIsFull.ObjectFromLresult(Marshal.ReadInt32(result), ref IID_IHTMLDocument, , out obj));
  10. }
  11.  
  12. Marshal.FreeHGlobal(result);
  13.  
  14. return obj as HTMLDocument;
  15. }

大致原理:

给IE窗体发送消息,获取到一个指向 IE浏览器(非托管)的某个内存块的指针,然后根据这个指针获取到HTMLDocument对象。

这个方法涉及到win32的两个函数:

  1. [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SendMessageTimeoutA")]
  2. public static extern int SendMessageTimeoutA(
  3. [InAttribute()] System.IntPtr hWnd,
  4. uint Msg, uint wParam, int lParam,
  5. uint fuFlags,
  6. uint uTimeout,
  7. System.IntPtr lpdwResult);
  1. [System.Runtime.InteropServices.DllImportAttribute("oleacc.dll", EntryPoint = "ObjectFromLresult")]
  2. public static extern int ObjectFromLresult(
  3. int lResult,
  4. ref Guid riid,
  5. int wParam,
  6. [MarshalAs(UnmanagedType.IDispatch), Out]
  7. out Object pObject
  8. );

从IE浏览器获取当前页面的内容的更多相关文章

  1. 转载: js jquery 获取当前页面的url,获取frameset中指定的页面的url(有修改)

    转载网址:http://blog.csdn.net/bestlxm/article/details/6800077 js jquery 怎么获取当前页面的url,获取frameset中指定的页面的ur ...

  2. [uiautomator篇] 获取当前页面的方法

    Uiautomator 在2.0之前的版本里就提供了getCurrentActivity()的方法,但返回内容不正确:2.0 版本今天尝试了下,还是返回有问题的: 有点没描述清楚啊,是在uiautom ...

  3. C#获取当前页面的url

    C#获取当前页面的url string a= Request.ApplicationPath; // / string b = Request.CurrentExecutionFilePath; // ...

  4. PHP中$_SERVER获取当前页面的完整URL地址

    PHP中$_SERVER获取当前页面的完整URL地址,其实很简单,主要是通过$_SERVER超全局变量来实现的. 具体PHP中$_SERVER获取当前页面的完整URL地址如下. #测试网址:     ...

  5. ASP.net获取当前页面的文件名,参数,域名等方法

    ASP.net后台获取当前页面的文件名 System.IO.Path.GetFileName(Request.Path).ToString(); 获取当前页面文件名,参数,域名等方法 假设当前页完整地 ...

  6. js获取当前页面的URL并且截取?之后的数据,返回json

    js获取当前页面的URL并且截取'?'之后的数据,返回json格式的数据 最近想要把学到的东西整理一下,以后方便查找,也是一种自我累积,如果有错误或者更好的,欢迎提出! 这篇文档主要是写关于获取页面的 ...

  7. js获取当前页面的url网址信息小汇总

    在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是我的一些获取url信息的小总结. 下面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblog ...

  8. 获取当前页面的URL信息

    以前在做网站的时候,经常会遇到当前页的分类高亮显示,以便让用户了解当前处于哪个页面.之前一直是在每个不同页面写方法.工程量大,也不便于修改.一直在想有什么简便的方法实现.后来在网上查到可以用获取当前U ...

  9. react获取当前页面的url参数

    react获取当前页面的url参数,必须在url路由对应的组件上获取,在子组件上获取不到,为undefined,获取形如  /news/:id  的后面的参数 id this.props.match. ...

随机推荐

  1. Python基础总结

      刚学习Python时,边学边总结的,采用思维导图的形式, 适合回顾使用.内容参考<Python:从入门到实践>一书.   再给出一张Datacamp网站上的一张关于Python基础的总 ...

  2. Python自动化--语言基础3--字典、函数、全局/局部变量

    字典 dict1 = {'name':'han','age':18,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values ...

  3. C#实现七牛云存储

    云存储,就是把本地的资源文件存放至网络上,可以公网访问.相当于网盘功能,感觉非常方便. 这里介绍的是七牛云存储.有兴趣的可以去官方网站详看 根据官网的介绍,本身是提供SDK的,下载地址,大家可以根据自 ...

  4. js “top、clientTop、scrollTop、offsetTop…”

    当要做一些与位置相关的插件或效果的时候,像top.clientTop.scrollTop.offsetTop.scrollHeight.clientHeight.offsetParent...看到这么 ...

  5. php中datetime时间和int时间互相转换

    int时间转换datetime时间 echo date("Y-m-d H:i:s", 1210003200);  datetime时间转换int时间 echo  strtotime ...

  6. 关于FFMPeg-PHP你必须要知道的

    #PHP FFmpeg [![Build Status](https://secure.travis-ci.org/PHP-FFMpeg/PHP-FFMpeg.png?branch=master)]( ...

  7. UVA - 11292 Dragon of Loowater 贪心

    贪心策略:一个直径为X的头颅,应该让雇佣费用满足大于等于X且最小的骑士来砍掉,这样才能使得花费最少. AC代码 #include <cstdio> #include <cmath&g ...

  8. C语言老司机学Python (四)

    字符串格式化: 可以使用类似c语言中sprintf函数的方法进行格式化,但是函数名称是print() 如:print('常量 PI 的值近似为:%5.3f.'  %  var_PI) 注意var_PI ...

  9. 使用C#解决部分Win8.1系统窗口每隔几秒失去焦点的问题【转】

    使用了Win8.1 With Update 1后,发现重启系统后,当前激活的窗口总是每隔几秒失去焦点,过0.5~1秒焦点回来,导致输入无法正常工作,严重影响使用心情和效率. 在网上找了很久,也没找到相 ...

  10. 网络基础tcp/ip协议四

    网络层的功能: 定义了基于ip协议的逻辑地址. 链接不同的媒介类型. 选择数据通过网络的最佳路劲. 数据包格式: 优先级与服务类型(8)位:优先级与服务类型 标识符,标志,段偏移量:这几个字用来对数据 ...