您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“c#如何從IE瀏覽器獲取當(dāng)前頁(yè)面的內(nèi)容”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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", "當(dāng)前頁(yè)面不是IE瀏覽器頁(yè)面,或使用了非IE內(nèi)核瀏覽器,如火狐,搜狗等。請(qǐng)使用IE瀏覽器打開(kāi)網(wǎng)頁(yè)"); } _getDocument = false; } else { _pointFrm.Left = MousePoint.X + 10; _pointFrm.Top = MousePoint.Y + 10; } } }
第11行的 GetHTMLDocumentFormHwnd(GetPointControl(MousePoint, false)) 分解下,先從鼠標(biāo)坐標(biāo)獲取頁(yè)面的句柄:
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; }
接下來(lái),根據(jù)句柄獲取頁(yè)面內(nèi)容:
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窗體發(fā)送消息,獲取到一個(gè)指向 IE瀏覽器(非托管)的某個(gè)內(nèi)存塊的指針,然后根據(jù)這個(gè)指針獲取到HTMLDocument對(duì)象。
這個(gè)方法涉及到win32的兩個(gè)函數(shù):
[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瀏覽器獲取當(dāng)前頁(yè)面的內(nèi)容”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。