c# 從IE瀏覽器獲取當(dāng)前頁面的內(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)前頁面不是IE瀏覽器頁面,或使用了非IE內(nèi)核瀏覽器,如火狐,搜狗等。請使用IE瀏覽器打開網(wǎng)頁");
}
_getDocument = false;
}
else
{
_pointFrm.Left = MousePoint.X + 10;
_pointFrm.Top = MousePoint.Y + 10;
}
}
}
第11行的 GetHTMLDocumentFormHwnd(GetPointControl(MousePoint, false)) 分解下,先從鼠標(biāo)坐標(biāo)獲取頁面的句柄:
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;
}
接下來,根據(jù)句柄獲取頁面內(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對象。
這個(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)前頁面的內(nèi)容的詳細(xì)內(nèi)容,更多關(guān)于c# 獲取瀏覽器頁面內(nèi)容的資料請關(guān)注腳本之家其它相關(guān)文章!
- C#實(shí)現(xiàn)清除IE瀏覽器緩存的方法
- C#實(shí)現(xiàn)基于IE內(nèi)核的簡單瀏覽器完整實(shí)例
- C# 利用Selenium實(shí)現(xiàn)瀏覽器自動(dòng)化操作的示例代碼
- C#瀏覽器提示跨域問題解決方案
- 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)簡易圖片瀏覽器的方法
- C#使用默認(rèn)瀏覽器打開網(wǎng)頁的方法
相關(guān)文章
C#自定義的方法實(shí)現(xiàn)堆棧類設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了如何使用C#創(chuàng)建一個(gè)帶有Push方法和Clist類的CStack類,并如何在其中添加和遍歷堆棧數(shù)據(jù),感興趣的可以了解下2024-03-03
C#如何利用結(jié)構(gòu)體對固定格式數(shù)據(jù)進(jìn)行解析
這篇文章主要為大家詳細(xì)介紹了C#利用結(jié)構(gòu)體對固定格式數(shù)據(jù)進(jìn)行解析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)
本篇文章主要介紹了c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
c# 9.0新特性nint和Pattern matching的使用方法
這篇文章主要介紹了c# 9.0新特性nint和Pattern matching的使用方法,文中講解非常細(xì)致,幫助你更好的學(xué)習(xí)c# 9.0,有需求的朋友可以參考下2020-06-06
C# 實(shí)現(xiàn)窗口無邊框,可拖動(dòng)效果
這篇文章主要介紹了C# 實(shí)現(xiàn)窗口無邊框,可拖動(dòng)效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-03-03

