最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 從IE瀏覽器獲取當(dāng)前頁(yè)面的內(nèi)容

 更新時(shí)間:2021年06月24日 09:52:49   作者:micDavid  
從IE瀏覽器獲取當(dāng)前頁(yè)面內(nèi)容可能有多種方式,今天我所介紹的是其中一種方法?;驹恚寒?dāng)鼠標(biāo)點(diǎn)擊當(dāng)前IE頁(yè)面時(shí),獲取鼠標(biāo)的坐標(biāo)位置,根據(jù)鼠標(biāo)位置獲取當(dāng)前頁(yè)面的句柄,然后根據(jù)句柄,調(diào)用win32的東西進(jìn)而獲取頁(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)文章!

相關(guān)文章

最新評(píng)論

手机| 文登市| 肥东县| 龙州县| 塔河县| 铁岭市| 北京市| 岳西县| 渝北区| 高州市| 英吉沙县| 霍林郭勒市| 炉霍县| 松阳县| 云阳县| 太仆寺旗| 汽车| 清流县| 西华县| 丰宁| 图片| 东乡族自治县| 景谷| 汽车| 永善县| 府谷县| 哈密市| 武隆县| 灵宝市| 水城县| 孝感市| 弋阳县| 阿巴嘎旗| 安新县| 安岳县| 县级市| 五河县| 民和| 衡阳市| 江津市| 巨鹿县|