C#擷取類名為Internet_Explorer_Server控制項的內容

來源:互聯網
上載者:User

      這篇文章的重點是講解如何擷取類名為Internet_Explorer_Server控制項的內容

      為了讓大家都能夠使用demo,我以IE為測試對象,另外為了突出重點,所以如何擷取視窗控制代碼我就不做示範了(不清楚的童鞋,可以去Google下哈),控制代碼值我使用spy++獲得

      大家可以下載demo(附:spy++)到本地進行調試學習,希望這篇文章對大家有所協助

知識點:

1.Internet_Explorer_Server

2.msthml

3.API:RegisterWindowMessage

4.API:ObjectFromLresult

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Diagnostics;using System.Threading;using mshtml;namespace 擷取類名Internet_Explorer_Server控制項的內容{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            if(String.IsNullOrEmpty(tbHandle.Text))            {                MessageBox.Show("請寫好視窗類別名為:Internet Explorer_Server的視窗控制代碼");                return;            }            mshtml.IHTMLDocument2 id = GetHtmlDocument(int.Parse(tbHandle.Text));            textBox1.Text = id.body.innerHTML;        }              class Win32API        {            [DllImport("user32", EntryPoint = "SendMessage")]            public static extern int SendMessage(int hwnd, int wMsg, int wParam, ref int lParam);            [DllImport("user32", EntryPoint = "RegisterWindowMessage")]            public static extern int RegisterWindowMessage(string lpString);            [DllImport("OLEACC.DLL", EntryPoint = "ObjectFromLresult")]            public static extern int ObjectFromLresult(                int lResult,                ref System.Guid riid,                int wParam,                [MarshalAs(UnmanagedType.Interface), System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out]ref System.Object ppvObject                //注意這個函數ObjectFromLresult的聲明            );        }        public mshtml.IHTMLDocument2 GetHtmlDocument(int hwnd)        {            System.Object domObject = new System.Object();            int tempInt = 0;            System.Guid guidIEDocument2 = new Guid();            int WM_Html_GETOBJECT =Win32API.RegisterWindowMessage("WM_Html_GETOBJECT");//定義一個新的視窗訊息            int W = Win32API.SendMessage(hwnd, WM_Html_GETOBJECT, 0, ref tempInt);//注:第二個參數是RegisterWindowMessage函數的傳回值            int lreturn = Win32API.ObjectFromLresult(W, ref guidIEDocument2, 0, ref domObject);            mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)domObject;            return doc;        }    }}

這裡,我以IE開啟的百度首頁為測試對象,效果如下:

 

原創文章,轉載請註明出處:http://www.cnblogs.com/hongfei/archive/2013/01/07/2849792.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.