日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C# 依据鼠标坐标取网页内成员坐标.ie

發(fā)布時(shí)間:2024/9/20 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 依据鼠标坐标取网页内成员坐标.ie 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C# 根據(jù)鼠標(biāo)坐標(biāo)取網(wǎng)頁(yè)內(nèi)成員坐標(biāo).ie

有時(shí)候你需要后臺(tái)獲取ie瀏覽器 鼠標(biāo)所在位置的元素坐標(biāo),然而你使用屏幕坐標(biāo)是不可行的

所以我們需要把坐標(biāo)轉(zhuǎn)換成瀏覽器內(nèi)坐標(biāo) 然后再通過(guò)elementFromPoint獲取網(wǎng)頁(yè)成員。

private void tmrWatcher_Tick(object sender, EventArgs e){IntPtr hWnd = WindowFromPoint(MousePosition);dynamic document = GetHtmlDocumentByHandle(hWnd);if (document != null){Rectangle r = GetHtmlElementPoint(hWnd, MousePosition, document); // 根據(jù)鼠標(biāo)坐標(biāo)取網(wǎng)頁(yè)成員坐標(biāo)Marshal.FinalReleaseComObject(document);Console.WriteLine(r.X + ":" + r.Y + ":" + r.Width + ":" + r.Height);}}

上面是一個(gè)時(shí)鐘tmrWatcher的Tick回調(diào)函數(shù),在上面使用了WindowFromPoint函數(shù) 主要是獲取

MousePosition所在的窗口句柄,然后再通過(guò)GetHtmlDocumentByHandle函數(shù)(獲取文檔從句柄)

public static object GetComObjectByHandle(int Msg, Guid riid, IntPtr hWnd){object _ComObject;int lpdwResult = 0;if (!SendMessageTimeout(hWnd, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, ref lpdwResult))return null;if (ObjectFromLresult(lpdwResult, ref riid, 0, out _ComObject))return null;return _ComObject;}public object GetHtmlDocumentByHandle(IntPtr hWnd){string buffer = new string('\0', 24);GetClassName(hWnd, ref buffer, 25);if (buffer != "Internet Explorer_Server")return null;return GetComObjectByHandle(WM_HTML_GETOBJECT, IID_IHTMLDocument, hWnd);}

實(shí)際上與我上次的帖子:http://blog.csdn.net/u012395622/article/details/46404193

并沒(méi)什么太大的出入,而獲取一個(gè)網(wǎng)頁(yè)文檔的成員只是簡(jiǎn)單的調(diào)度Mshtml COM接口

public Rectangle GetHtmlElementPoint(IntPtr hWnd, Point point, dynamic document){if (document == null && hWnd != IntPtr.Zero)return Rectangle.Empty;ScreenToClient(hWnd, ref point);dynamic element = document.elementFromPoint(point.X, point.Y);if (element == null) return Rectangle.Empty;try{Rectangle o = new Rectangle(){Y = element.offsetTop,X = element.offsetLeft,Width = element.offsetWidth,Height = element.offsetHeight};while (element.offsetParent != null){element = element.offsetParent;o.Y += element.offsetTop;o.X += element.offsetLeft;}return o;}catch{return Rectangle.Empty;}}

上面代碼是實(shí)現(xiàn)獲取 元素在網(wǎng)頁(yè)內(nèi)的一個(gè)確切坐標(biāo),整體并不是很難閱讀的。

之所以while(element.offsetParent != null) { ... }是因?yàn)榫W(wǎng)頁(yè)始終與客戶端不相

同我們不能用常規(guī)在Win32操作控件位置那樣去看待它 它很麻煩,而且層次

很難分明,所以會(huì)造成你根本不知道到底有多寬不過(guò)還好,一般計(jì)算一個(gè)

成員元素在窗口什么位置,只要把父容器的位置加起來(lái)就行了。反正有點(diǎn)

解釋的不清楚,大家莫見(jiàn)怪

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]public static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);[DllImport("user32.dll", SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)]public static extern bool GetClassName([In]IntPtr hWnd,[MarshalAs(UnmanagedType.VBByRefStr)]ref string IpClassName,[In]int nMaxCount);[DllImport("oleacc.dll", SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)]public static extern bool ObjectFromLresult([In]int lResult,[In]ref Guid riid,[In]int wParam,[Out, MarshalAs(UnmanagedType.IUnknown)]out object ppvObject);[DllImport("user32.dll", SetLastError = true)][return: MarshalAs(UnmanagedType.I4)]public static extern int RegisterWindowMessage([In]string lpString);[DllImport("user32.dll", EntryPoint = "SendMessageTimeoutA", SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)]public static extern bool SendMessageTimeout([In]IntPtr MSG,[In]int hWnd,[In]int wParam,[In]int lParam,[In]int fuFlags,[In]int uTimeout,[In, Out]ref int lpdwResult);[DllImport("user32.dll", SetLastError = true)][return: MarshalAs(UnmanagedType.SysInt)]public static extern IntPtr WindowFromPoint([In]Point Point);public const int SMTO_ABORTIFHUNG = 2;public readonly static int WM_HTML_GETOBJECT = RegisterWindowMessage("WM_HTML_GETOBJECT"); public readonly static Guid IID_IHTMLDocument = new Guid("626fc520-a41e-11cf-a731-00a0c9082637");

總結(jié)

以上是生活随笔為你收集整理的C# 依据鼠标坐标取网页内成员坐标.ie的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。