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瀏覽器獲取當前頁面的內容的詳細內容,更多關於c# 獲取瀏覽器頁面內容的資料請關註WalkonNet其它相關文章!

推薦閱讀: