c# 從IE瀏覽器獲取當(dāng)前頁(yè)面的內(nèi)容
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)容的詳細(xì)內(nèi)容,更多關(guān)于c# 獲取瀏覽器頁(yè)面內(nèi)容的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C#實(shí)現(xiàn)清除IE瀏覽器緩存的方法
- C#實(shí)現(xiàn)基于IE內(nèi)核的簡(jiǎn)單瀏覽器完整實(shí)例
- C# 利用Selenium實(shí)現(xiàn)瀏覽器自動(dòng)化操作的示例代碼
- C#瀏覽器提示跨域問(wèn)題解決方案
- C# 模擬瀏覽器并自動(dòng)操作的實(shí)例代碼
- C#導(dǎo)出pdf的實(shí)現(xiàn)方法(瀏覽器不預(yù)覽直接下載)
- C# WinForm實(shí)現(xiàn)圖片瀏覽器
- C#文件下載實(shí)例代碼(適用于各個(gè)瀏覽器)
- C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件
- C#編程實(shí)現(xiàn)簡(jiǎn)易圖片瀏覽器的方法
- C#使用默認(rèn)瀏覽器打開(kāi)網(wǎng)頁(yè)的方法
相關(guān)文章
C#自定義的方法實(shí)現(xiàn)堆棧類設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了如何使用C#創(chuàng)建一個(gè)帶有Push方法和Clist類的CStack類,并如何在其中添加和遍歷堆棧數(shù)據(jù),感興趣的可以了解下2024-03-03C#實(shí)現(xiàn)微信退款及對(duì)賬功能的示例詳解
在招聘報(bào)名系統(tǒng)里,考務(wù)費(fèi)支付是其中一個(gè)環(huán)節(jié),支付方式很多種,比如銀聯(lián)、微信、支付寶等等,本次我們以微信支付進(jìn)行舉例,在實(shí)際的應(yīng)用中,對(duì)于支付成功的考生,我們會(huì)遇到實(shí)現(xiàn)退款的需求,所以本文給大家介紹了使用C#實(shí)現(xiàn)微信退款及對(duì)賬,需要的朋友可以參考下2023-11-11C#如何利用結(jié)構(gòu)體對(duì)固定格式數(shù)據(jù)進(jìn)行解析
這篇文章主要為大家詳細(xì)介紹了C#利用結(jié)構(gòu)體對(duì)固定格式數(shù)據(jù)進(jìn)行解析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)
本篇文章主要介紹了c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01c# 9.0新特性nint和Pattern matching的使用方法
這篇文章主要介紹了c# 9.0新特性nint和Pattern matching的使用方法,文中講解非常細(xì)致,幫助你更好的學(xué)習(xí)c# 9.0,有需求的朋友可以參考下2020-06-06C# 實(shí)現(xiàn)窗口無(wú)邊框,可拖動(dòng)效果
這篇文章主要介紹了C# 實(shí)現(xiàn)窗口無(wú)邊框,可拖動(dòng)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-03-03C#判斷數(shù)據(jù)類型的簡(jiǎn)單示例代碼
本篇文章要是對(duì)C#中判斷數(shù)據(jù)類型的簡(jiǎn)單示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01