返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >c# 从IE浏览器获取当前页面的内容
  • 811
分享到

c# 从IE浏览器获取当前页面的内容

2024-04-02 19:04:59 811人浏览 薄情痞子
摘要

private void timer1_Tick(object sender, EventArgs e) { lock (currentL


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浏览器获取当前页面的内容的详细内容,更多关于c# 获取浏览器页面内容的资料请关注编程网其它相关文章!

--结束END--

本文标题: c# 从IE浏览器获取当前页面的内容

本文链接: https://lsjlt.com/news/129020.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作