Click here to Skip to main content
15,896,557 members
Articles / Web Development / HTML

SnatchIE

Rate me:
Please Sign up or sign in to vote.
3.67/5 (5 votes)
9 Nov 20051 min read 29.2K   785   20  
A web browsing utility which snatches the addresses of all currently opened Internet Explorer web browser windows in real-time.
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using SHDocVw;

namespace SnatchIE
{
	/// <summary>
	/// Summary description for SnatchIE.
	/// </summary>
	public class SnatchIE : Form
	{
		private System.Windows.Forms.ToolBar toolBar;
		private System.Windows.Forms.ToolBarButton toolBarButtonRefresh;
		private System.Windows.Forms.ToolBarButton toolBarButtonAutoSync;
		private System.Windows.Forms.ToolBarButton toolBarButtonSeparator0;
		private System.Windows.Forms.ToolBarButton toolBarButtonSave;
		private System.Windows.Forms.ToolBarButton toolBarButtonOpen;
		private System.Windows.Forms.ToolBarButton toolBarButtonSeparator1;
		private System.Windows.Forms.ToolBarButton toolBarButtonAbout;
		private System.Windows.Forms.ImageList imageList;
		private ListView listViewUrl;
		private ColumnHeader columnHeaderName;
		private ColumnHeader columnHeaderURL;
		private OpenFileDialog openFileDialog;
		private SaveFileDialog saveFileDialog;
		private IContainer components;

		private ShellWindows shellWindows;
		private InternetExplorer browser;

		private ArrayList IEHandles = new ArrayList();

		private bool isAutoSync = true;
		public bool IsAutoSync
		{
			get { return isAutoSync; }
			set { isAutoSync = value; }
		}

		public SnatchIE()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			
			this.shellWindows = new ShellWindowsClass();
			this.shellWindows.WindowRegistered += new DShellWindowsEvents_WindowRegisteredEventHandler(this.shellWindows_WindowRegistered);
			this.shellWindows.WindowRevoked += new DShellWindowsEvents_WindowRevokedEventHandler(this.shellWindows_WindowRevoked);

			this.browser = new InternetExplorer();
			this.browser.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.browser_BeforeNavigate2);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SnatchIE));
			this.listViewUrl = new System.Windows.Forms.ListView();
			this.columnHeaderName = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderURL = new System.Windows.Forms.ColumnHeader();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			this.toolBar = new System.Windows.Forms.ToolBar();
			this.toolBarButtonRefresh = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonAutoSync = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonSeparator0 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonSave = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonOpen = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonSeparator1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonAbout = new System.Windows.Forms.ToolBarButton();
			this.imageList = new System.Windows.Forms.ImageList(this.components);
			this.SuspendLayout();
			// 
			// listViewUrl
			// 
			this.listViewUrl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.listViewUrl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.listViewUrl.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						  this.columnHeaderName,
																						  this.columnHeaderURL});
			this.listViewUrl.FullRowSelect = true;
			this.listViewUrl.GridLines = true;
			this.listViewUrl.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.listViewUrl.Location = new System.Drawing.Point(0, 28);
			this.listViewUrl.MultiSelect = false;
			this.listViewUrl.Name = "listViewUrl";
			this.listViewUrl.Size = new System.Drawing.Size(292, 145);
			this.listViewUrl.TabIndex = 1;
			this.listViewUrl.View = System.Windows.Forms.View.Details;
			this.listViewUrl.DoubleClick += new System.EventHandler(this.listViewUrl_DoubleClick);
			// 
			// columnHeaderName
			// 
			this.columnHeaderName.Text = "Name";
			this.columnHeaderName.Width = 39;
			// 
			// columnHeaderURL
			// 
			this.columnHeaderURL.Text = "URL";
			this.columnHeaderURL.Width = 253;
			// 
			// openFileDialog
			// 
			this.openFileDialog.DefaultExt = "xml";
			this.openFileDialog.Filter = "XML Files|*xml";
			// 
			// saveFileDialog
			// 
			this.saveFileDialog.DefaultExt = "xml";
			this.saveFileDialog.Filter = "XML Files|*xml";
			// 
			// toolBar
			// 
			this.toolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					   this.toolBarButtonRefresh,
																					   this.toolBarButtonAutoSync,
																					   this.toolBarButtonSeparator0,
																					   this.toolBarButtonSave,
																					   this.toolBarButtonOpen,
																					   this.toolBarButtonSeparator1,
																					   this.toolBarButtonAbout});
			this.toolBar.Cursor = System.Windows.Forms.Cursors.Hand;
			this.toolBar.DropDownArrows = true;
			this.toolBar.ImageList = this.imageList;
			this.toolBar.Location = new System.Drawing.Point(0, 0);
			this.toolBar.Name = "toolBar";
			this.toolBar.ShowToolTips = true;
			this.toolBar.Size = new System.Drawing.Size(292, 28);
			this.toolBar.TabIndex = 2;
			this.toolBar.Wrappable = false;
			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
			// 
			// toolBarButtonRefresh
			// 
			this.toolBarButtonRefresh.ImageIndex = 0;
			this.toolBarButtonRefresh.ToolTipText = "Refresh";
			// 
			// toolBarButtonAutoSync
			// 
			this.toolBarButtonAutoSync.ImageIndex = 1;
			this.toolBarButtonAutoSync.Pushed = true;
			this.toolBarButtonAutoSync.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
			this.toolBarButtonAutoSync.ToolTipText = "AutoSync";
			// 
			// toolBarButtonSeparator0
			// 
			this.toolBarButtonSeparator0.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarButtonSave
			// 
			this.toolBarButtonSave.ImageIndex = 2;
			this.toolBarButtonSave.ToolTipText = "Save";
			// 
			// toolBarButtonOpen
			// 
			this.toolBarButtonOpen.ImageIndex = 3;
			this.toolBarButtonOpen.ToolTipText = "Open";
			// 
			// toolBarButtonSeparator1
			// 
			this.toolBarButtonSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarButtonAbout
			// 
			this.toolBarButtonAbout.ImageIndex = 4;
			this.toolBarButtonAbout.ToolTipText = "About";
			// 
			// imageList
			// 
			this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.imageList.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// SnatchIE
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(292, 173);
			this.Controls.Add(this.toolBar);
			this.Controls.Add(this.listViewUrl);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "SnatchIE";
			this.Text = "SnatchIE";
			this.Load += new System.EventHandler(this.SnatchIE_Load);
			this.ResumeLayout(false);

		}
		#endregion

		public static void Main()
		{
			Application.Run(new SnatchIE());
		}

		private void IE_Start(string url)
		{
			Process p = new Process();
			p.StartInfo.FileName = "iexplore.exe";
			p.StartInfo.Arguments = url;
			p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
			p.Start();
		}

		private void listViewUrl_Load()
		{
			try
			{
				this.listViewUrl.Items.Clear();
			}
			catch (Exception e)
			{
				
			}
            
			foreach (InternetExplorer ie in this.shellWindows)
			{
				if (ie.LocationName != string.Empty)
				{
					ListViewItem listViewItem = new ListViewItem(ie.LocationName);
					listViewItem.SubItems.Add(ie.LocationURL);

					this.listViewUrl.Items.Add(listViewItem);

					foreach (ColumnHeader ch in listViewUrl.Columns)
					{
						ch.Width = -2;
					}
				}
			}
		}

		private void browser_BeforeNavigate2(object a, ref object b, ref object c, ref object d, ref object e, ref object f, ref bool g)
		{
			// Browser URL
			//b.ToString();

			if (this.IsAutoSync)
			{
				this.listViewUrl_Load();
				this.listViewUrl_Load();
			}
		}

		// Browser window opened.
		private void shellWindows_WindowRegistered(int z)
		{
			string filename;

			foreach (InternetExplorer ie in shellWindows)
			{
				filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

				if (filename.Equals("iexplore"))
				{
					browser = ie;
					bool b = true;

					for (int i = 0; i < IEHandles.Count; i++)
					{
						if (IEHandles[i].ToString() == browser.HWND.ToString())
						{
							b = false;
						}
					}

					if (b)
					{
						IEHandles.Add(browser.HWND.ToString());

						browser.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(browser_BeforeNavigate2);
					}
				}
			}
		}

		// Browser window closed.
		private void shellWindows_WindowRevoked(int z)
		{
			string filename;
			ArrayList IEHandlesClosed = new ArrayList();

			foreach (InternetExplorer ie in shellWindows)
			{
				filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

				if (filename.Equals("iexplore"))
				{
					IEHandlesClosed.Add(ie.HWND.ToString());
				}
			}

			for (int i = 0; i < this.IEHandles.Count; i++)
			{
				bool b = false;

				for (int j = 0; j < IEHandlesClosed.Count; j++)
				{
					if (Convert.ToInt32(this.IEHandles[i]) == Convert.ToInt32(IEHandlesClosed[j]))
					{
						b = true;
					}
				}

				if (b)
				{
					this.IEHandles.RemoveAt(i);

					break;
				}
			}
		}

		private void SnatchIE_Load(object sender, EventArgs e)
		{
			this.listViewUrl_Load();
		}

		private void toolBarButtonRefresh_Click()
		{
			this.listViewUrl_Load();
		}

		private void toolBarButtonAutoSync_Click()
		{
			this.IsAutoSync = this.toolBarButtonAutoSync.Pushed;
		}

		private void toolBarButtonSave_Click()
		{
			XmlDocument xmlDocument = new XmlDocument();

			XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);

			XmlElement rootNode = xmlDocument.CreateElement("root");
			xmlDocument.InsertBefore(xmlDeclaration, xmlDocument.DocumentElement);
			xmlDocument.AppendChild(rootNode);

			foreach (ListViewItem listViewItem in listViewUrl.Items)
			{
				XmlElement xmlElement = xmlDocument.CreateElement("node");

				xmlElement.SetAttribute("Name", listViewItem.SubItems[0].Text);
				xmlElement.SetAttribute("Url", listViewItem.SubItems[1].Text);

				xmlDocument.DocumentElement.PrependChild(xmlElement);
			}

			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				xmlDocument.Save(saveFileDialog.FileName);
			}
		}

		private void toolBarButtonOpen_Click()
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				XmlTextReader xmlTextReader = new XmlTextReader(openFileDialog.FileName);
				xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;
				xmlTextReader.MoveToContent();

				ListViewItem listViewItem = null;

				while (xmlTextReader.Read())
				{
					xmlTextReader.MoveToFirstAttribute();
					listViewItem = new ListViewItem(xmlTextReader.Value);

					xmlTextReader.MoveToNextAttribute();
					listViewItem.SubItems.Add(xmlTextReader.Value);

					//this.listViewUrl.Items.Add(listViewItem);

					if (xmlTextReader.Value.Length > 0)
					{
						this.IE_Start(xmlTextReader.Value);
					}
				}

				foreach (ColumnHeader ch in listViewUrl.Columns)
				{
					ch.Width = -2;
				}
			}
		}

		private void toolBarButtonAbout_Click()
		{
			About about = new About();
			about.ShowDialog();
		}

		private void listViewUrl_DoubleClick(object sender, EventArgs e)
		{
			ListViewItem listViewItem = this.listViewUrl.SelectedItems[0];

			this.IE_Start(listViewItem.SubItems[1].Text);
		}

		private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
		{
			switch (this.toolBar.Buttons.IndexOf(e.Button))
			{
				case 0:
					this.toolBarButtonRefresh_Click();
					break; 
				case 1:
					toolBarButtonAutoSync_Click();
					break; 
				case 2:
					break;
				case 3:
					this.toolBarButtonSave_Click();
					break;
				case 4:
					this.toolBarButtonOpen_Click();
					break;
				case 5:
					break;
				case 6:
					this.toolBarButtonAbout_Click();
					break;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions