实例介绍
【实例简介】IE浏览器监控程序,有详细的代码注释与说明
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | 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 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; } } } |
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论