從IE瀏覽器獲取當前頁面的內容


      從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 + 10;
52                     _pointFrm.Top = MousePoint.Y + 10;
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);
 4             Object obj = null;
 5 
 6             Console.WriteLine(Win32APIsFull.SendMessageTimeoutA(hwnd, HTML_GETOBJECT_mid, 0, 0, 2, 1000, result));
 7             if (Marshal.ReadInt32(result) != 0)
 8             {
 9                 Console.WriteLine(Win32APIsFull.ObjectFromLresult(Marshal.ReadInt32(result), ref IID_IHTMLDocument, 0, out obj));
10             }
11 
12             Marshal.FreeHGlobal(result);
13 
14             return obj as HTMLDocument;
15         }

 

大致原理:

 

給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
        );

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM