C#读取IE窗口信息

IE编程——读取IE窗口信息

目标:

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

实现:

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

        2. 获得IE窗口信息。 

添加引用

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;
        }

    }
}

 

取得的IE窗口如下:

C#读取IE窗口信息

                                  

        取得Windows资源浏览器的窗口如下:

Windows资源浏览器的窗口

来源:本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一、小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!

加支付宝好友偷能量挖...


评论(0)网络
阅读(108)喜欢(0)Asp.Net/C#/WCF