您的位置:首页 > 编程语言 > C#

C#读取IE窗口信息

2013-04-14 10:18 246 查看
IE编程——读取IE窗口信息

目标:

程序自动读取所有正在运行的IE(6.0或7.0)窗口信息,如窗口句柄HWND、状态文本StatusText、名字Name、路径Path等。

实现:

1. 添加对COM组件Microsoft Internet Controls的引用,如下图。

2. 获得IE窗口信息。



-收缩
C#
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Data.OleDb;

namespace TestIWebBrowser
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}

private void explorerBtn_Click(object sender, EventArgs e)
{
resultTextBox.Text = TestSHDocVwDll.ReadExplorerInfo();
}

private void browserBtn_Click(object sender, EventArgs e)
{
resultTextBox.Text = TestSHDocVwDll.ReadIEInfo();
}
}

public class TestSHDocVwDll
{
public static string ReadIEInfo()
{
string strText = string.Empty;

SHDocVw.IShellWindows sw = new SHDocVw.ShellWindowsClass();
for (int i = 0; i < sw.Count; i++)
{
SHDocVw.IWebBrowser2 browser = sw.Item(i) as SHDocVw.IWebBrowser2;

if (browser != null && browser.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
{
strText += "HWND : " + String.Format("{0:X}", browser.HWND) + "/r/n";
strText += "StatusText : " + browser.StatusText + "/r/n";
strText += "visible : " + browser.Visible.ToString() + "/r/n";
strText += "Name : " + browser.Name + "/r/n";
strText += "Path : " + browser.Path + "/r/n";
strText += "FullName : " + browser.FullName + "/r/n";
strText += "LocationName: " + browser.LocationName + "/r/n";
strText += "LocationURL : " + browser.LocationURL + "/r/n/r/n";
}
}

return strText;
}

public static string ReadExplorerInfo()
{
string strText = string.Empty;

SHDocVw.IShellWindows sw = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in sw)
{
//if it is windows explorer
if (ie.FullName.ToUpper().IndexOf("EXPLORER.EXE") > 0)
{
strText += "HWND : " + String.Format("{0:X}", ie.HWND) + "/r/n";
//strText += "StatusText : " + ie.StatusText + "/r/n";
strText += "visible : " + ie.Visible.ToString() + "/r/n";
strText += "Name : " + ie.Name + "/r/n";
strText += "Path : " + ie.Path + "/r/n";
strText += "FullName : " + ie.FullName + "/r/n";
strText += "LocationName: " + ie.LocationName + "/r/n";
strText += "LocationURL : " + ie.LocationURL + "/r/n/r/n";
}
}

//or coding as follows
//for (int i = 0; i < sw.Count; i++)
//{
// SHDocVw.InternetExplorer ie = sw.Item(i) as SHDocVw.InternetExplorer;
// //...
//}

return strText;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: