基本信息
源码名称:IE浏览器监控程序源码
源码大小:0.97M
文件格式:.rar
开发语言:C#
更新时间:2019-10-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
IE浏览器监控程序,有详细的代码注释与说明

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//------------------------------------------------------
using System.Collections;
using System.IO;//File.AppendAllText (String, String)
using System.Reflection;
using System.Runtime.InteropServices;
//------------------------------------------------------
using System.Threading;//使用延时

using SHDocVw;
using MSHTML;//程序原来是小写mshtml  升级了win10 这里必须改为大写MSHTML

using System.Web;

namespace WindowsFormsApplication1
{
    public partial class frmListenToIE : Form
    {
        public frmListenToIE()
        {
            InitializeComponent();
        }

        //IE监控器对象_ieWatcher 
        private IE_Watcher _ieWatcher = null;

        private void frmListenToIE_Load(object sender, EventArgs e)
        {

            //实例化对象_ieWatcher,将窗体对象作为参数传入? 
            _ieWatcher = new IE_Watcher(this);
            //定义动态资料记录器对象 
            //_ieWatcher.UrlText = new UrlHistory("d:\\urlhistory.txt");
            _ieWatcher.UrlText = new UrlHistory(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase   "urlhistory.txt");//在程序运行目录下生成访问url记录文件
            //btnGetWindow_Click(sender, e);//点击获取ie浏览器打开的网页的列表,把获取ie浏览器打开的网页的列表写到listBox1中

        }

        private void btnGetWindow_Click(object sender, EventArgs e)
        {
            btnGetWindow.Enabled = false;//先设置按钮不可用,防止连续点击,多次启动ie

            //调用系统启动ie浏览器并访问url 
            System.Diagnostics.Process.Start("iexplore.exe", "http://127.0.0.1/mainpage.html");

            Thread.Sleep(8000);//程序暂停8秒,等待操作系统启动ie,再向下执行,否则iexplore.exe可能还没完全运行起来,导致下面获取不到ie的url列表
            //获取所有已打开的IE窗体 
            IList windows = _ieWatcher.GetOpenedWindows();//获取ie浏览器打开的网页的列表

            foreach (SHDocVw.InternetExplorer ie in windows)
            {
                //在这里加入自定义对象
                listBox1.Items.Add(new IE_Item(ie));//把获取ie浏览器打开的网页的列表写到listBox1中
            }

            //选中listBox1的第一个,点击按钮进行ie事件监听
            if (listBox1.Items.Count >=1)
            {
                listBox1.SelectedIndex=0;//设置选中第一行,本程序中只有一行,如果是多用户切换是可以是多行
                btnListen_Click(sender, e);//开始监视ie
            }
            

        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            //btnListen.Enabled = false;//先设置按钮不可用,防止连续点击
            //开始监视你选中的IE窗体 
            if (listBox1.SelectedIndex >= 0)
            {
                //将选中的Item转换为IE_Item对象 
                IE_Item item = listBox1.SelectedItem as IE_Item;
                //item.IE 
                _ieWatcher.ListenWindow(item.IE, this.ShowLog);//把listBox1作为对象传给_ieWatcher;把this.ShowLog处理过程传给_ieWatcher,ShowLog修改listBox2
                                                               //这样,_ieWatcher可以访问listBox1与listBox2
                ShowLog("开始监视ie......");
            }
        }

        //显示IE动态消息 
        public void ShowLog(string content)
        {
            listBox2.Items.Add(content);
            //下面两句设置listBox2滚动显示到最后一行,便于查看最新的web访问url状态
            listBox2.SelectedIndex = listBox2.Items.Count - 1;
            listBox2.Focus();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = listBox2.Text;
        }

    }

    //IE窗口监视器
    public class IE_Watcher
    {
        private Form _owner = null;

        public IE_Watcher(Form owner)
        {
            _owner = owner;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr GetForegroundWindow();          //WINAPI 获取当前活动窗体的句柄 

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern bool SetForegroundWindow(IntPtr hWnd); //WINAPI 设置当前活动窗体的句柄 

        //跨线程访问主线程内的Log控件,自定义委托 
        public delegate void ShowLogHandle(string msg);
        //Txt文件记录器 
        private UrlHistory _urltext = null;
        public UrlHistory UrlText { get { return _urltext; } set { _urltext = value; } }


        //获取IE浏览器打开的网页列表: GetOpenedWindows()返回值是list
        public IList GetOpenedWindows()
        {
            IList list = new ArrayList();

            //注:要引入SHDocVw COM对象哦!!! Add Refernces - > COM Objects -> Microsft Internet Controls 
            //1、Microsoft Internet Controls
            //2、Microsoft HTML Object Library
            //Assembly Interop.SHDocVw 
            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();//old:SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
            string filename;
            foreach (SHDocVw.InternetExplorer ie in shellWindows)
            {
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                //注:iexplore, 如Maxthon浏览器 
                if (filename.Equals("iexplore")) list.Add(ie);
            }

            return list;
        }

        //当前监控的IE窗体对象 
        private SHDocVw.InternetExplorer _ie = null;
        public SHDocVw.InternetExplorer CurrentIE { get { return _ie; } }

        //显示动态消息委托,用于跨线程 
        private ShowLogHandle _showLog = null;

        //开始监控IE窗体 
        public void ListenWindow(SHDocVw.InternetExplorer ie, ShowLogHandle showLog)
        {
            _ie = ie;//  在IE_Watcher中_ie是全局变量,定义是:private SHDocVw.InternetExplorer _ie = null;
            _showLog = showLog;

            //置顶被监控的IE窗体 
            if (new IntPtr(ie.HWND) != GetForegroundWindow()) SetForegroundWindow(new IntPtr(ie.HWND));

            //绑定事件: IE打开网页时触发这个事件 
            ie.NavigateComplete2  = new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(OnNavigateComplete);
            ie.TitleChange  = new SHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(OnTitleChange);
            
            //绑定其他事件与事件处理:用户提交数据
            ie.BeforeNavigate2  = new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(OnBeforeNavigate2);   
        }

        
        //获取用户post的数据
        private void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
        {
            //throw new NotImplementedException();
            string postDataText="";

            if (PostData != null)
            {
                //UTF8编码的Byte[]转换为String类型:
                //byte[] byteArray = new byte[100];
                //String str = System.Text.Encoding.UTF8.GetString(byteArray);
                postDataText = System.Text.Encoding.UTF8.GetString(PostData as byte[]); //响应 BeforeNavigate2 事件
                                                                                        //下面需要进行UTF8 urldecode解码postDataText
                                                                                        //using System.Web;
                postDataText = System.Web.HttpUtility.UrlDecode(postDataText, System.Text.Encoding.UTF8);//将Url中的UTF8编码转换为简体汉字
                //System.Windows.Forms.MessageBox.Show(postDataText);
            }

            if (_urltext != null)
            {
                _urltext.WriteHistory("posturl :" URL.ToString());                            //写入文本文件
                if(postDataText.Length >0)_urltext.WriteHistory("postdata:" postDataText);    //写入用户提交的数据post数据
                this.ShowMsg("打开:"   URL.ToString());                                       //窗口显示动态消息
                if (postDataText.Length > 0) this.ShowMsg("postdata:"   postDataText);        //显示用户提交的数据
            }

        }

        //打开网页完成的处理 
        private void OnNavigateComplete(object pDisp, ref object URL)
        {
            if (_urltext != null)
            {
                _urltext.WriteHistory("打开:"   URL.ToString());  //写入文本文件 
                this.ShowMsg("打开:"   URL.ToString());           //显示动态消息 
            }

            HtmlDocument doc = _ie.Document;
            int iCount = doc.Window.Frames.Count;

            for (int i = 0; i < iCount; i  )
            {
                string strurlscr = doc.Window.Frames[i].Url.ToString();
                this.ShowMsg("iframe:"   strurlscr);


            }

        }

        public void OnTitleChange(string Text)
        {
            this.ShowMsg("标题:"   Text);
        }

        //跨线程访问主线程内的Log控件,给控件赋值 
        private void ShowMsg(string msg)
        {
            _owner.Invoke(_showLog, msg);
        }
    }

    //将IE窗体动态消息写文件
    /// <summary> 
    /// Txt文件记录器 
    /// </summary> 
    public class UrlHistory
    {
        private string _file;

        public UrlHistory(string file)
        {
            _file = file;
        }

        public void WriteHistory(string content)
        {
            File.AppendAllText(_file, content   "\r\n");
        }
    }


    //在ListBox控件Items属性插入自定义对象所定义的类
    /// <summary> 
    /// IE窗体对象,用于在ListBox内显示 
    /// </summary> 
    public class IE_Item
    {
        private SHDocVw.InternetExplorer _ie = null;
        public SHDocVw.InternetExplorer IE { get { return _ie; } }

        public string UrlName { get { return _ie.LocationName; } }

        public string UrlAddress { get { return _ie.LocationURL; } }

        public IE_Item(SHDocVw.InternetExplorer ie)
        {
            _ie = ie;
        }

        //关键是这个重载的方法哦!!! 在ListBox内显示的资料  
        public override string ToString()
        {
            return _ie.LocationName;
        }
    }


}