您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“c#如何从IE浏览器获取当前页面的内容”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
private void timer1_Tick(object sender, EventArgs e) { lock (currentLock) { System.Drawing.Point MousePoint = System.Windows.Forms.Form.MousePosition; if (_leftClick) { timer1.Stop(); _leftClick = false; _lastDocument = GetHTMLDocumentFormHwnd(GetPointControl(MousePoint, false)); if (_lastDocument != null) { if (_getDocument) { _getDocument = true; try { string url = _lastDocument.url; string html = _lastDocument.documentElement.outerHTML; string cookie = _lastDocument.cookie; string domain = _lastDocument.domain; var resolveParams = new ResolveParam { Url = new Uri(url), Html = html, PageCookie = cookie, Domain = domain }; RequetResove(resolveParams); } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } } } else { new MessageTip().Show("xx", "当前页面不是IE浏览器页面,或使用了非IE内核浏览器,如火狐,搜狗等。请使用IE浏览器打开网页"); } _getDocument = false; } else { _pointFrm.Left = MousePoint.X + 10; _pointFrm.Top = MousePoint.Y + 10; } } }
第11行的 GetHTMLDocumentFormHwnd(GetPointControl(MousePoint, false)) 分解下,先从鼠标坐标获取页面的句柄:
public static IntPtr GetPointControl(System.Drawing.Point p, bool allControl) { IntPtr handle = Win32APIsFull.WindowFromPoint(p); if (handle != IntPtr.Zero) { System.Drawing.Rectangle rect = default(System.Drawing.Rectangle); if (Win32APIsFull.GetWindowRect(handle, out rect)) { return Win32APIsFull.ChildWindowFromPointEx(handle, new System.Drawing.Point(p.X - rect.X, p.Y - rect.Y), allControl ? Win32APIsFull.CWP.ALL : Win32APIsFull.CWP.SKIPINVISIBLE); } } return IntPtr.Zero; }
接下来,根据句柄获取页面内容:
public static HTMLDocument GetHTMLDocumentFormHwnd(IntPtr hwnd) { IntPtr result = Marshal.AllocHGlobal(4); Object obj = null; Console.WriteLine(Win32APIsFull.SendMessageTimeoutA(hwnd, HTML_GETOBJECT_mid, 0, 0, 2, 1000, result)); if (Marshal.ReadInt32(result) != 0) { Console.WriteLine(Win32APIsFull.ObjectFromLresult(Marshal.ReadInt32(result), ref IID_IHTMLDocument, 0, out obj)); } Marshal.FreeHGlobal(result); return obj as HTMLDocument; }
大致原理:
给IE窗体发送消息,获取到一个指向 IE浏览器(非托管)的某个内存块的指针,然后根据这个指针获取到HTMLDocument对象。
这个方法涉及到win32的两个函数:
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SendMessageTimeoutA")] public static extern int SendMessageTimeoutA( [InAttribute()] System.IntPtr hWnd, uint Msg, uint wParam, int lParam, uint fuFlags, uint uTimeout, System.IntPtr lpdwResult);
[System.Runtime.InteropServices.DllImportAttribute("oleacc.dll", EntryPoint = "ObjectFromLresult")] public static extern int ObjectFromLresult( int lResult, ref Guid riid, int wParam, [MarshalAs(UnmanagedType.IDispatch), Out] out Object pObject );
“c#如何从IE浏览器获取当前页面的内容”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。