C#获取类名为Internet_Explorer_Server控件的内容
生活随笔
收集整理的這篇文章主要介紹了
C#获取类名为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打開的百度首頁為測試對象,效果如下:
總結
以上是生活随笔為你收集整理的C#获取类名为Internet_Explorer_Server控件的内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农村赚钱新商机 看一看哪个适合你
- 下一篇: 东京奥运会今日启幕 一个注定赔钱的