Wednesday, 28 September 2011

How to find a url of Active tab from Active Internet Explore Instance

First Add new class name as IEInstance.cs

The code for this class

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
public delegate bool IECallBack(int hwnd, int lParam);
namespace IEToolbarEngine
    public class IEInstance//Form is a class in System.Windows.Forms namespace
        public static extern int EnumWindows(IECallBack x, int y);
        public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
        public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);
        public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        public static ListBox lbPublic;
        private ListBox listBox1; //ListBox is a class in System.Windows.Forms namespace
        static IntPtr listBoxHandle;// IntPtr is a class in System namespace
        static IntPtr windowHandle;
        static StringBuilder sb, sbc;
        static int i = 0;
        static ArrayList myAl;

        public IEInstance() //Constructor

        #region Windows Form Designer generated code
        private void InitializeComponent()
           // this.GetIE = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            myAl = new ArrayList();

        public void GetIEButton()
            listBoxHandle = listBox1.Handle;
            EnumWindows(new IECallBack(IEInstance.EnumWindowCallBack), (int)listBoxHandle);

        private  static bool EnumWindowCallBack(int hwnd, int lParam)
            windowHandle = (IntPtr)hwnd;
            listBoxHandle = (IntPtr)lParam;
            lbPublic = (ListBox)ListBox.FromHandle(listBoxHandle);
            sb = new StringBuilder(1024);
            sbc = new StringBuilder(256);
            GetClassName(hwnd, sbc, sbc.Capacity);
            GetWindowText((int)windowHandle, sb, sb.Capacity);
            int ind = sb.ToString().IndexOf("- Microsoft");
                if (ind ==-1)
                    ind = sb.ToString().IndexOf("- Windows");
            String xMsg2 = string.Empty;
            if (ind != -1)
                xMsg2 = sb.ToString().Substring(0,ind - 0) + " " + windowHandle;
                xMsg2 = sb + " " + windowHandle;
            String xMsg = windowHandle.ToString();
            if (sbc.Length > 0)
                if (sbc.ToString().Equals("IEFrame"))
                    if (lbPublic.Items.Count < 1)
            return true;


I have inherited this class with form class .In "lbPublic"  .This is a ListBox type . After executing this class your lbPublic will fill the active tab title + handler of the tab. You will just add this whole code in your class file ,no need to extent more.

Now , for the sample Add a form in which you will call the above class method GetIEButton();
In a form include a COM component reference MICROSOFT HTML OBJECT LIBRARY.

After adding reference include Add the below line the using area
using mshtml;

Now in the form Load method

private void Form1_Load(object sender, EventArgs e)
        //// Below is the IEInstance class object
           IEInstance ieButton = new IEInstance();

            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
            string filename = string.Empty; ;

            foreach (SHDocVw.InternetExplorer ie in shellWindows)
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2;
                if (htmlDoc != null)
                    string strIe = htmlDoc.title + "  " + ie.HWND.ToString();
                    //htmlDoc = (HTMLDocument)axWebBrowser1.Document;
                    if (filename.Equals("iexplore"))
                            if (IEInstance.lbPublic.Items[0].ToString() == strIe)
                                filename = ie.LocationURL;

                                //  break;
                                goto NewLine;


                //Console.WriteLine( &quot;Web Site   : {0}", ie.LocationURL );

                if (filename.Equals("explorer&quot;"))
                    Console.WriteLine("Hard Drive : {0}", ie.LocationURL);

            int i2 = 0;


This is in working state .now you will get thr url of active tab of active Internet Explorer .

No comments:

Post a Comment