您的位置:首页 > 运维架构

IE浏览器监控程序-监控IE窗体URL动态加载网页等信息

2018-01-23 11:30 393 查看
本程序由SHDocVw COM实现的。在项目工程内加入COM 引用:
Add Refernces - > COM Objects -> Microsft Internet Controls

主要用到SHDocVw.ShellWindowsCOM接口 ,SHDocVw.ShellWindowsClass类,
及SHDocVw.InternetExplorer 接口。

最重要的是IE窗口InternetExplorer接口,该接口继承IWebBrowser2. 

IWebBrowser2接口metadata资料如下:

using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace SHDocVw

{

   [Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E")]

   [TypeLibType(4176)]

   public interface IWebBrowser2 : IWebBrowserApp

   {

      [DispId(555)]

      bool AddressBar { get; set;
}

      [DispId(200)]

      object Application { get; }

      [DispId(212)]

      bool Busy { get; }

      [DispId(202)]

      object Container { get; }

      [DispId(203)]

      object Document { get; }

      [DispId(400)]

      string FullName { get; }

      [DispId(407)]

      bool FullScreen { get; set;
}

      [DispId(209)]

      int Height { get; set;
}

      [DispId(-515)]

      int HWND { get; }

      [DispId(206)]

      int Left { get; set;
}

      [DispId(210)]

      string LocationName { get; }

      [DispId(211)]

      string LocationURL { get; }

      [DispId(406)]

      bool MenuBar { get; set;
}

      [DispId(0)]

      string Name { get; }

      [DispId(550)]

      bool Offline { get; set;
}

      [DispId(201)]

      object Parent { get; }

      [DispId(401)]

      string Path { get; }

      [DispId(-525)]

      tagREADYSTATE ReadyState { get; }

      [DispId(552)]

      bool RegisterAsBrowser { get; set;
}

      [DispId(553)]

      bool RegisterAsDropTarget { get; set;
}

      [DispId(556)]

      bool Resizable { get; set;
}

      [DispId(551)]

      bool Silent { get; set;
}

      [DispId(403)]

      bool StatusBar { get; set;
}

      [DispId(404)]

      string StatusText { get; set;
}

      [DispId(554)]

      bool TheaterMode { get; set;
}

      [DispId(405)]

      int ToolBar { get; set;
}

      [DispId(207)]

      int Top { get; set;
}

      [DispId(204)]

      bool TopLevelContainer { get; }

      [DispId(205)]

      string Type { get; }

      [DispId(402)]

      bool Visible { get; set;
}

      [DispId(208)]

      int Width { get; set;
}

      

      [DispId(301)]

      void ClientToWindow(ref int pcx, ref int pcy);

      [DispId(502)]

      void ExecWB(OLECMDID cmdID, OLECMDEXECOPT cmdexecopt, ref object pvaIn, ref object pvaOut);

      [DispId(303)]

      object GetProperty(string Property);

      [DispId(100)]

      void GoBack();

      [DispId(101)]

      void GoForward();

      [DispId(102)]

      void GoHome();

      [DispId(103)]

      void GoSearch();

      [DispId(104)]

      void Navigate(string URL, ref object Flags, ref object TargetFrameName, ref object PostData, refobject Headers);

      [DispId(500)]

      void Navigate2(ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, refobject Headers);

      [DispId(302)]

      void PutProperty(string Property, object vtValue);

      [DispId(501)]

      OLECMDF QueryStatusWB(OLECMDID cmdID);

      [DispId(300)]

      void Quit();

      [DispId(-550)]

      void Refresh();

      [DispId(105)]

      void Refresh2(ref object Level);

      [DispId(503)]

      void ShowBrowserBar(ref object pvaClsid, ref object pvarShow, ref object pvarSize);

      [DispId(106)]

      void Stop();

   }

}

看看吧,IWebBrowser2接到口内是不是有很多我们关心的方法和属性? 接下来看实现吧!

IE_Watcher类:取名为IE窗口监视器, 啥好听的名字自己取呗!

//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浏览器打开的网页列表 

   public IList GetOpenedWindows()

   {

      IList list = new ArrayList();

      

      //注:要引入SHDocVw COM对象哦!!! Add Refernces - > COM Objects -> Microsft Internet Controls 

      //Assembly Interop.SHDocVw 

      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;

      _showLog = showLog;

      

      //置顶被监控的IE窗体 

      if (new IntPtr(ie.HWND) != GetForegroundWindow()) SetForegroundWindow(new IntPtr(ie.HWND));

      

      //绑定事件: IE打开网页时触发这个事件 

      ie.NavigateComplete2 += newSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(OnNavigateComplete);

      

      

      ie.TitleChange += new SHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(OnTitleChange);

   }

   

   public void OnTitleChange(string Text)

   {

      this.ShowMsg("标题:" + Text);

   }

   

   //打开网页完成 

   private void OnNavigateComplete(object pDisp, ref object URL)

   {

      if (_urltext != null)

      {

         _urltext.WriteHistory(URL.ToString()); //写入文本文件 

         

         this.ShowMsg("打开:" + URL.ToString()); //显示动态消息 

      }

   }

   

   //跨线程访问主线程内的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;

   }

}

好了,测试窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace TestProject

{

   public partial class frmListenToIE : Form

   {

      //IE窗体监控器 

      private IE_Watcher _ieWatcher = null;

      

      public frmListenToIE()

      {

         InitializeComponent();

      }

      

      private void frmListenToIE_Load(object sender,
EventArgs e)

      {

         //实例化对象,将窗体对象作为参数传入,为什么呢? 

         _ieWatcher = new IE_Watcher(this);

         

         //定义动态资料记录器对象 

         _ieWatcher.UrlText = new UrlHistory("c:\\urlhistory.txt");

      }

      

      private void btnGetWindow_Click(object sender,
EventArgs e)

      {

         //获取所有已打开的IE窗体 

         IList windows = _ieWatcher.GetOpenedWindows();

         

         foreach (SHDocVw.InternetExplorer ie in windows)

         {

            //在这里加入自定义对象哦!!! 

            listBox1.Items.Add(new IE_Item(ie));

         }

      }

      

      private void btnListen_Click(object sender,
EventArgs e)

      {

         //开始监视你选中的IE窗体 

         if (listBox1.SelectedIndex >= 0)

         {

            //将选中的Item转换为IE_Item对象 

            IE_Item item = listBox1.SelectedItem as IE_Item;

            

            //item.IE 

            _ieWatcher.ListenWindow(item.IE, this.ShowLog);

         }

      }

      

      //显示IE动态消息 

      public void ShowLog(string content)

      {

         listBox2.Items.Add(content);

         listBox2.SelectedIndex = listBox2.Items.Count - 1;

         listBox2.Focus();

      }

   }

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