通过IE句柄 获得 IE Document对象和IWebBrowser2对象

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using mshtml; using SHDocVw; using System.Runtime.InteropServices;namespace dxzj { public partial class Form1 : Form { [DllImport("user32", EntryPoint = "FindWindow")] public static extern int FindWindowA(string lpClassName, string lpWindowName); public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { } //声明Document对象(如果用内嵌浏览器,我们得到的是一个HTMLDocument) HTMLDocumentClass document = null; private void button1_Click(object sender, EventArgs e) { //查找打开的窗口句柄 int iehwnd = FindWindowA(null, "用银行卡充值_中国联通 - Microsoft Internet Explorer"); //初始化所有IE窗口 IShellWindows sw = new ShellWindowsClass(); //轮询所有IE窗口 for (int i = sw.Count - 1; i >= 0; i--) { //得到每一个IE的 IWebBrowser2 对象 IWebBrowser2 iwb2 = sw.Item(i) as IWebBrowser2; //比对 得到的 句柄是否符合查找的窗口句柄 if(iwb2.HWND == iehwnd) { //查找成功 进行赋值 document = (HTMLDocumentClass)iwb2.Document; //对网页进行操作 document.getElementById("directOnlinePayInfo.productNO").innerText = "1111"; } } } } }

你可能感兴趣的:(C#,lab)