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
    {
        [DllImport("user32.Dll")]
        public static extern int EnumWindows(IECallBack x, int y);
        [DllImport("User32.Dll")]
        public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
        [DllImport("User32.Dll")]
        public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);
        [DllImport("User32.Dll")]
        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
        {
            InitializeComponent();
        }

        #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();
        }
        #endregion

        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;
            else
                xMsg2 = sb + " " + windowHandle;
            String xMsg = windowHandle.ToString();
            if (sbc.Length > 0)
            {
                if (sbc.ToString().Equals("IEFrame"))
                {
                    myAl.Add(windowHandle);
                    i++;
                    if (lbPublic.Items.Count < 1)
                        lbPublic.Items.Add(xMsg2);
                }
            }
            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();
            ieButton.GetIEButton();


            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);
            }

        NewLine:
            int i2 = 0;
            MessageBox.Show(filename);

       }


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


No comments:

Post a Comment