Proposed Answer IE7, IE8 Tab Interaction - Accessibility Class

  • Saturday, January 24, 2009 10:28 PM
     
     

     Here's a C# Class for IE7 + IE8 Tabs (using Accessibility) - no ShellWindows

    Tested with XP and IE7 + IE8.

    Usage: Add a reference to Accessibility

    IEAccessible tabs = new IEAccessible();

    var active = tabs.GetActiveTabIndex(yourIEMainHWND);

    Methods Available:

    GetActiveTabIndex

    GetActiveTabCaption

    Activate Tab by Index or Caption

    GetTabCount

    GetTabCaptions


    using System;

    using System.Collections.Generic;

    using Accessibility;

    using System.Runtime.InteropServices;

    using System.Diagnostics;

     

    namespace IEAccessibleTabs

    {

        public class IEAccessible

        {

            private enum OBJID : uint

            {

                OBJID_WINDOW = 0x00000000,

     

            }

            private const int IE_ACTIVE_TAB = 2097154;

            private const int CHILDID_SELF = 0;

            private IAccessible accessible;

            private IEAccessible[] Children

            {

                get

                {

                    int num = 0;

                    object[] res = GetAccessibleChildren(accessible, out num);

                    if (res == null)

                        return new IEAccessible[0];

     

                    List<IEAccessible> list = new List<IEAccessible>(res.Length);

                    foreach (object obj in res)

                    {

                        IAccessible acc = obj as IAccessible;

                        if (acc != null)

                            list.Add(new IEAccessible(acc));

                    }

                    return list.ToArray();

                }

            }

            private string Name

            {

                get

                {

                    string ret = accessible.get_accName(CHILDID_SELF);

                    return ret;

                }

            }

            private int ChildCount

            {

                get

                {

                    int ret = accessible.accChildCount;

                    return ret;

                }

            }

     

            public IEAccessible()

            {

     

            }

            public IEAccessible(IntPtr ieHandle, string tabCaptionToActivate)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                        {

                            if (tab.Name == tabCaptionToActivate)

                            {

                                tab.Activate();

                                return;

                            }

                        }

                    }

                }

            }

            public IEAccessible(IntPtr ieHandle, int tabIndexToActivate)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var index = 0;

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                        {

                            if (tabIndexToActivate >= child.ChildCount -1) return;

                            if (index == tabIndexToActivate)

                            {

                                tab.Activate();

                                return;

                            }

                            index++;

                        }

                    }

                }

            }

            private IEAccessible(IntPtr ieHandle)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

            }

            public string GetActiveTabUrl(IntPtr ieHandle)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                        {

                            object tabIndex = tab.accessible.get_accState(CHILDID_SELF);

     

                            if ((int)tabIndex == IE_ACTIVE_TAB)

                            {

                                var description = tab.accessible.get_accDescription(CHILDID_SELF);

     

                                if (!string.IsNullOrEmpty(description))

                                {

                                    if (description.Contains(Environment.NewLine))

                                    {

                                        var url = description.Substring(description.IndexOf(Environment.NewLine)).Trim();

                                        return url;

                                    }

                                }

                            }

                        }

                    }

                }

     

                return String.Empty;

            }

            public int GetActiveTabIndex(IntPtr ieHandle)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var index = 0;

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                        {

                            object tabIndex = tab.accessible.get_accState(0);

     

                            if ((int)tabIndex == IE_ACTIVE_TAB)

                                return index;

     

                            index++;                  

                        }

                    }

                }

     

                return -1;

            }

            public string GetActiveTabCaption(IntPtr ieHandle)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                        {

                            object tabIndex = tab.accessible.get_accState(0);

     

                            if ((int)tabIndex == IE_ACTIVE_TAB)

                              return tab.Name;

                        }

                    }

                }

                return String.Empty;

            }

            public List<string> GetTabCaptions(IntPtr ieHandle)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                var captionList = new List<string>();

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                        foreach (var tab in child.Children)

                            captionList.Add(tab.Name);

     

                }

                if (captionList.Count > 0)

                    captionList.RemoveAt(captionList.Count - 1);

     

                return captionList;

     

            }

            public int GetTabCount(IntPtr ieHandle)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                            return child.ChildCount - 1;

                    }

                }

                return 0;

     

            }

     

            private IntPtr GetDirectUIHWND(IntPtr ieFrame)

            {

                var directUI = FindWindowEx(ieFrame, IntPtr.Zero, "CommandBarClass", null);

                directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);

                directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);

                directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);

                return directUI;

     

            }

            private IEAccessible(IAccessible acc)

            {

                if (acc == null)

                    throw new Exception();

     

                accessible = acc;

            }

            private void Activate()

            {

                accessible.accDoDefaultAction(CHILDID_SELF);

     

            }

            private static object[] GetAccessibleChildren(IAccessible ao, out int childs)

            {

                childs = 0;

                object[] ret = null;

                int count = ao.accChildCount;

                if (count > 0)

                {

                    ret = new object[count];

                    AccessibleChildren(ao, 0, count, ret, out childs);

                }

                return ret;

            }

     

     

            #region Interop

            [DllImport("user32.dll", SetLastError = true)]

            private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,

            string lpszWindow);

            private static int AccessibleObjectFromWindow(IntPtr hwnd, OBJID idObject, ref IAccessible acc)

            {

                Guid guid = new Guid("{618736e0-3c3d-11cf-810c-00aa00389b71}"); // IAccessible

     

                object obj = null;

                int num = AccessibleObjectFromWindow(hwnd, (uint)idObject, ref guid, ref obj);

                acc = (IAccessible)obj;

                return num;

            }

            [DllImport("oleacc.dll")]

            private static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint id, ref Guid iid, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);

            [DllImport("oleacc.dll")]

            private static extern int AccessibleChildren(IAccessible paccContainer, int iChildStart, int cChildren, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] object[] rgvarChildren, out int pcObtained);

            #endregion

        }

     

     

    }

     

    • Edited by cablehead Saturday, January 24, 2009 10:38 PM edit
    • Edited by cablehead Sunday, January 25, 2009 5:36 PM edit
    • Edited by cablehead Sunday, January 25, 2009 5:37 PM edit
    • Edited by cablehead Sunday, January 25, 2009 5:38 PM edit
    • Edited by cablehead Monday, February 09, 2009 6:04 PM edit
    • Edited by cablehead Monday, June 01, 2009 2:25 PM
    •  

All Replies

  • Saturday, May 30, 2009 5:35 AM
     
     

    Hi cablehead,

    I hope your still watching this thread. I have been able to implement this code in my app and I can see that it will work with IE8 aswell. I haven't tested it with Vista but as it dosen't use shellexecute so it should work. 

    I need a method to close a tab with a given tabcaption. I have been trying to sendmessage with WM_CLOSE, WM_DESTROY, WM_QUIT, QM_NOTIFYPARENT etc with no joy. I tried to close TabWindowClass (which is now "Frame Tab" in IE8) which closes the DOC but still leaves the DirectUIHWND visable. closing the DriectUIHWND kills all tabs but leaves the DOC's visable.

    Do you know a way of closing a given tab.

    Better yet is there a way to have a method return a WebBrowser2 interface or a IHTMLDocument2 or even a HDocVw.InternetExplorer given a IEMainHWND via the Accessibility class.

    Thanks,

    TAHAIC

    P.S. I am a little lost in how the Accessibility class is working - perhaps you could repost the code with some comments.


  • Saturday, May 30, 2009 9:59 PM
     
     
    There is a way to get WebBrowser2 from a handle. You need the handle of Internet Explorer_Server of the tab. (Not IEFrame) If you need the code let me know.
    Looking for "entry level" position...
  • Saturday, May 30, 2009 11:55 PM
     
     
    Thanks for responding,

    I have been looking at using AccessibleObjectFromWindow function to get WebBrowser2 interface.

    Firstly I'm not sure how to marshal it. The standard parameters are:


    [DllImport("oleacc.dll")]

    private static extern int AccessibleObjectFromWindow(IntPtr hwnd, int dwObjectID, ref Guid riid, IntPtr ppvObject);

    but you have:

    [DllImport("oleacc.dll")]

    private static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint id, ref Guid iid, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);

    Can you tell me what is going on here?

    Reading the documentation I not sure what the objectID needs to be OBJID_WINDOW or OBJID_SELF for example... the refID I think needs to be IID_IDispatch

    MSDN quote

    "The IWebBrowser2 interface derives from IDispatch indirectly; that is, IWebBrowser2 derives from IWebBrowserApp, which in turn derives from IWebBrowser, which finally derives from IDispatch. "

    And then I'm not sure how to use it, or if I need to cast the result etc.


    when you say I need the handle of the tab, is that DirectUIHWND or TabWindowClass? The DirectUIHWND seam to be the whole group which I guess has children for each individual tab ...in which case how do I get the tab handle.

     

     

    As you can see I am struggling with this so YES, I need the code, if you would be so kind.

    Thanks

  • Sunday, May 31, 2009 1:20 AM
     
     
    So far I have come with this for closing a tab.

    I noticed that each Tab has a child "Close Tab" - so I added the following:

     public void IEAccessibleCloseTab(IntPtr ieHandle, string tabCaptionToClose)

            {

                AccessibleObjectFromWindow(GetDirectUIHWND(ieHandle), OBJID.OBJID_WINDOW, ref accessible);

                if (accessible == null)

                    throw new Exception();

     

                var ieDirectUIHWND = new IEAccessible(ieHandle);

     

                foreach (IEAccessible accessor in ieDirectUIHWND.Children)

                {

                    foreach (var child in accessor.Children)

                    {

                        foreach (var tab in child.Children)

                        {

                            if (tab.Name == tabCaptionToActivate)

                            {

                                foreach (var  CloseTab in tab.Children)
                                   CloseTab.Activate();

                                return;

                            }

                        }

                    }

                }

            }

    Which works!!



    I still need help with getting the WebBrowser2 interface.
  • Sunday, May 31, 2009 6:27 PM
     
     

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

            public static extern uint RegisterWindowMessage(string lpString);

            [DllImport("oleacc.dll", PreserveSig = false)]

            [return: MarshalAs(UnmanagedType.Interface)]

            public static extern object ObjectFromLresult(UIntPtr lResult,

            [MarshalAs(UnmanagedType.LPStruct)] Guid refiid, IntPtr wParam);

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

            public static extern IntPtr SendMessageTimeout(

                IntPtr hWnd,

                uint Msg,

                UIntPtr wParam,

                UIntPtr lParam,

                SendMessageTimeoutFlags fuFlags,

                uint uTimeout,

                out UIntPtr lpdwResult);

     

            [Flags]

            public enum SendMessageTimeoutFlags : uint

            {

                SMTO_NORMAL = 0x0000,

                SMTO_BLOCK = 0x0001,

                SMTO_ABORTIFHUNG = 0x0002,

                SMTO_NOTIMEOUTIFNOTHUNG = 0x0008

            }

     

            private IHTMLDocument2 GetIEDocumentFromHwnd(IntPtr ieServerHwnd)

            {

                UIntPtr lResult;

                IHTMLDocument2 htmlDocument = null;

     

                if (ieServerHwnd != IntPtr.Zero)

                {

     

                    var lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");

     

                    SendMessageTimeout(ieServerHwnd, lMsg, UIntPtr.Zero, UIntPtr.Zero,

     

                    SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lResult);

                    if (lResult != UIntPtr.Zero)

                    {

     

                        htmlDocument = ObjectFromLresult(lResult,

                        typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;

                        if (htmlDocument == null)

                        {

                            throw new COMException("Unable to cast the object");

     

                        }

                    }

                }

     

                return htmlDocument;

            }

        }


    Looking for "entry level" position...
  • Thursday, July 30, 2009 1:59 PM
     
     
    Hi Cablehead,

    It was verymuch helpfull for me. Is it possible to add new tab and close existing tab?
  • Thursday, July 30, 2009 3:48 PM
     
     
    New Tab:

    Just navigate from your IWebbrowser instance with the navOpenInNewTab const.

    Tab closing code is posted in this thread by TAHAIC.
    Looking for "entry level" position...
  • Friday, July 31, 2009 12:06 PM
     
     
    Hi Cablehead,

    You Rock!
    I would like to say thanks for your help.
  • Wednesday, August 05, 2009 8:30 PM
     
     
    Hi Cablehead,

    I tried this code with my toolbar (bandobject based), but can't get it to work, here is my code

    IEAccessibleTabs.

    IEAccessible tabs = new IEAccessibleTabs.IEAccessible();

     

    List<string> list = tabs.GetTabCaptions((IntPtr) Explorer.HWND);

    when I debugged into GetTabCaptions(), looks like "GetDirectUIHWND(ieHandle)" is able to find the DirectUIHWND under IEFrame, but each ieDirectUIHWND.Children doesn't find any child.

    My question is the "Tab" in IE8/7 is of class "TabWindowClass" and is the child of "Frame Tab", which is the child of IEFrame, how is it related to the "DirectUIHWND" under tabBandClass?

    Thank you!

  • Wednesday, August 19, 2009 3:38 PM
     
      Has Code
    Hi all,

    The GetIEDocumentFromHwnd(IntPtr) didn't works for me....

    I'm using another technique for enumerate Windows (Browsers).

    public static IDictionary<HWND, string> GetOpenWindows()
            {
                HWND lShellWindow = GetShellWindow();
                Dictionary<HWND, string> lWindows = new Dictionary<HWND, string>();
    
                EnumWindows(delegate(HWND hWnd, int lParam)
                {
                    if (hWnd == lShellWindow) return true;
                    if (!IsWindowVisible(hWnd)) return true;
    
                    int lLength = GetWindowTextLength(hWnd);
                    if (lLength == 0) return true;
    
                    StringBuilder lBuilder = new StringBuilder(lLength);
                    GetWindowText(hWnd, lBuilder, lLength + 1);
                    //RealGetWindowClass(hWnd, lBuilder, (uint)lLength + 1);
    	       ///Here, i can compare Window Class to get only IEFrames
                    lWindows[hWnd] = lBuilder.ToString();
    
                    return true;
    
                }, 0);
    
                return lWindows;
            }
    
            delegate bool EnumWindowsProc(HWND hWnd, int lParam);
    
            [DllImport("USER32.DLL")]
            static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
    
            [DllImport("USER32.DLL")]
            static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);
    
            [DllImport("USER32.dll")]
            static extern uint RealGetWindowClass(IntPtr hWnd, StringBuilder pszType, uint cchType);
    
            [DllImport("USER32.DLL")]
            static extern int GetWindowTextLength(HWND hWnd);
    
            [DllImport("USER32.DLL")]
            static extern bool IsWindowVisible(HWND hWnd);
    
            [DllImport("USER32.DLL")]
            static extern IntPtr GetShellWindow();
    Then, i loop into this Dictionary<IntPtr, string> variable to get IHTMLDocumento from Window Handle.

    Do you know if exists any difference between Process.MainWindowHandle and EnumWindows handle?

    It returns null always

    There is my code...

    foreach (KeyValuePair<IntPtr, string> lWindow in OpenWindowGetter.GetOpenWindows())
                {
                    IntPtr lHandle = lWindow.Key;
                    string lTitle = lWindow.Value;
    
                    IHTMLDocument2 doc = tabs.GetIEDocumentFromHwnd(lHandle);
    
                    if (null != doc)
                        break;
                }
    Thanks for your reply.
  • Wednesday, August 19, 2009 5:11 PM
     
     Proposed Answer
    2 things:

    Vista protected mode won't allow Shellwindows...

    GetDocumentfromHWND needs the Internet_Explorer_Server hwnd associated with the current tab. (Not the main IEFrame class)


    Will work for gourmet food...
    • Proposed As Answer by Brian_Garnica Wednesday, August 19, 2009 7:52 PM
    •  
  • Wednesday, August 19, 2009 7:52 PM
     
     
    cablehead, Great Job. You are DA man. It works!!!!
  • Wednesday, August 19, 2009 8:24 PM
     
     
    cablehead, Great Job. You are DA man. It works!!!!

    By chance, do you know how to get SHDocVw.InternetExplorer or IWebBrowserApp?

    I want to subscribe to document's controls events.

    Thanks again.
  • Thursday, August 20, 2009 3:17 PM
     
     

    Can you help me why tabs.GetTabCaptions((IntPtr) Explorer.HWND) is not working in my case?

    thanks a lot!

  • Friday, August 21, 2009 7:24 PM
     
     

    Can you help me why tabs.GetTabCaptions((IntPtr) Explorer.HWND) is not working in my case?

    thanks a lot!


    How is "Explorer" object created?
  • Monday, August 24, 2009 5:37 PM
     
     

    it comes from BandObject project from codeproject.com. The Explorer object is obtained from setSite():

    public virtual void SetSite(Object pUnkSite)
      {
                if (BandObjectSite != null)
                    //Marshal.ReleaseComObject( BandObjectSite );
                    Marshal.FinalReleaseComObject(BandObjectSite);

       if( Explorer != null )
       {
        //Marshal.ReleaseComObject( Explorer );
                    Marshal.FinalReleaseComObject(Explorer);
        Explorer = null;
       }

       BandObjectSite = (IInputObjectSite)pUnkSite;
       if( BandObjectSite != null )
       {
        //pUnkSite is a pointer to object that implements IOleWindowSite or something  similar
        //we need to get access to the top level object - explorer itself
        //to allows this explorer objects also implement IServiceProvider interface
        //(don't mix it with System.IServiceProvider!)
        //we get this interface and ask it to find WebBrowserApp
        _IServiceProvider sp = BandObjectSite as _IServiceProvider;
        Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
        Guid riid = ExplorerGUIDs.IID_IUnknown;
        
        try
        {
         object w;
         sp.QueryService(
          ref guid,
          ref riid,
          out w );
        
         //once we have interface to the COM object we can create RCW from it
         Explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
          w as IWebBrowser,
          typeof(WebBrowserClass)
          );

         OnExplorerAttached(EventArgs.Empty);
        }
        catch( COMException )
        {
         //we anticipate this exception in case our object instantiated
         //as a Desk Band. There is no web browser service available.
        }
       }

               
      }

  • Friday, August 28, 2009 3:33 AM
     
     Proposed Answer
    using Accessibility;

    Where is this assenbly.  How can I get It.
    Thx
    • Proposed As Answer by Phoenix371 Wednesday, December 16, 2009 1:44 PM
    •  
  • Wednesday, December 16, 2009 1:46 PM
     
     
    Don't know if anyone is still checking this thread, but you need to add a reference and select Accessibility, which is at the top I believe
  • Thursday, July 01, 2010 3:05 PM
     
     

    Hi,

    I tried out your ie accessible class. but the code from TAHAIC doesn't work. "close tab" child for each tab doesnt exists!

    Any other idea to close any tab? Maybe I typed something wrong, but I dont think so. I have the access to all tabs in the foreach loop but no "close Tab" child exists.

    Thank you for your response...

  • Friday, July 16, 2010 5:45 PM
     
     

    Hi,

    I tried out your ie accessible class. but the code from TAHAIC doesn't work. "close tab" child for each tab doesnt exists!

    Any other idea to close any tab? Maybe I typed something wrong, but I dont think so. I have the access to all tabs in the foreach loop but no "close Tab" child exists.

    Thank you for your response...


    You need to activate the tab you want to close first. Only the active tab has a Close Button (use UISpy to see this behaviour)

    What I'd like to know is how to go about hooking into all the tabs in an IE window.

    Using this code, you can hook the active tabs, but it leaves you with no way to hook into new tabs. The event that triggers (NewWindow2) when you open a link in a new window has an empty ppDisp variable, so no way to know which one you need to use.

    For Each ie As SHDocVw.InternetExplorer In New SHDocVw.ShellWindows

     

     

    If ie.Name = "Windows Internet Explorer" Then

     

     

    Debug.WriteLine(ie.FullName)

     

     

    Debug.WriteLine(ie.Application)

     

     

    Debug.WriteLine(CType(ie.Document, mshtml.HTMLDocument).referrer)

     

     

    AddHandler ie.TitleChange, AddressOf NewIETitle

     

     

    AddHandler ie.BeforeNavigate2, AddressOf BeforeNav ' Werkt niet

     

     

    AddHandler ie.DocumentComplete, AddressOf DocumentComplete

     

     

    AddHandler ie.NewWindow2, AddressOf NewWindow2 ' ppDisp blijft nothing

     

     

    AddHandler ie.NewWindow3, AddressOf NewWindow3 ' Hit niet

     

     

    Console.WriteLine(ie.HWND & " -> " & ie.LocationURL & " - " & ie.LocationName)

     

     

    End If

     

     

    Next

  • Monday, November 15, 2010 3:13 PM
     
     

    hello cablehead

    1)In browser there are 5  tabs.among them i want to focus any required tab when required tab is not selected.How should i implement that.i am using asp.net

    2)How do i instantiate the IEAccessible  class in vb.net(Low Priority).

    Thanks

  • Thursday, March 03, 2011 10:05 PM
     
     

    Hi cablehead,

       Ours is a WPF user control and wrapped using winforms control as a activeX control and hosted in IE.Currently i am testing our application in IE8 browser and all childwindows are loosing modality w.r.t IE 8 browser.

       could you please tell me how do i get the HWND of IE browser tab where our app is loaded.

     

    Thanks,

     

     

  • Monday, March 21, 2011 8:02 AM
     
     

    Hello all,

     

    Anyone having success with this method on IE9? The code can't find the "TabBandClass" Class hwnd anymore!

     

    Best wishes,
    Apex75

  • Monday, March 21, 2011 7:00 PM
     
     

    Never mind, found it myself! Remove the first two find windows for IE9...

    Like this:

                var directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass"null);

                directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND"null);

                return directUI;

  • Tuesday, March 22, 2011 3:05 PM
     
     

    Never mind, found it myself! Remove the first two find windows for IE9...

    Like this:

     

                var directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass"null);

                directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND"null);

                return directUI;

     

    This won't work at : FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null); because directUI isn't declared.

    Could you please tell me how you did to have it working? It always return a null value with IE 9 .. :/


    Thanks!

  • Thursday, April 07, 2011 8:04 PM
     
      Has Code

    I am having the same problem with ie9, i made the following adjustment but still not working

          var directUI = FindWindowEx(ieFrame, IntPtr.Zero, "TabBandClass", null);
          directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
          return directUI;
    

     

    Anyone have any suggestions?

    thanks

  • Friday, April 08, 2011 7:22 PM
     
     

    Hi CableHead and others

    I know this is IE forum but i want to know if this Accessibility class work with firefox. I tried but it didn't work. Below are the 2 classname i found on firefox using spy++

    childControl ClassName = MozillaWindowClass
     application ClassName = MozillaUIWindowClass

    Any hints or ideas on how to enumerate through firefox tabs, get the tabs text and current tab url

    thanks kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

  • Monday, April 18, 2011 7:08 AM
     
     

    I am too having problem in IE9 as the accessible value is always null. How can I fix it?

    Any  help is appreciated.

     

    Thanks,

    Binaya

  • Thursday, April 21, 2011 7:59 AM
     
     Proposed Answer Has Code

    Hi,

    The problem in IE9 can be fixed using the following.

     

     private IntPtr GetDirectUIHWND(IntPtr ieFrame)
     {
      // For IE 8:
      var directUI = FindWindowEx(ieFrame, IntPtr.Zero, "CommandBarClass", null);
      directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
      directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
      directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
    
      if (directUI == IntPtr.Zero)
      {
      // For IE 9:
      directUI = FindWindowEx(ieFrame, IntPtr.Zero, "WorkerW", "Navigation Bar");
      directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
      directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
      directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
      }
    
      return directUI;
     }
    

    I got this solution in the following link:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7be1e51d-6b78-41aa-a4a0-e4f20bdaafd5

    Hope this helps.

    Regards,

    Binaya

  • Monday, July 11, 2011 11:44 PM
     
      Has Code

    Does anyone has an idea how to automate the "Close other tabs" action?

    Activating all tabs (except the initially focused) in turn and closing them one-by-one is unacceptable in my situation.

    The best I've achieved so far is:

     

       IntPtr activeTabHwnd = _ieTabWrapper.GetActiveTabButtonHwnd();
       WinFormsUtils.ForceForegroundWindow(activeTabHwnd);
       User32Dll.SetFocus(activeTabHwnd);
       
       // Simulate Ctrl+Alt+F4
       User32Dll.keybd_event((byte)Keys.ControlKey, 0, 0, UIntPtr.Zero);
       User32Dll.keybd_event((byte)Keys.Menu, 0, 0, UIntPtr.Zero);
       User32Dll.keybd_event((byte)Keys.F4, 0, 0, UIntPtr.Zero);
    
       WaitHelper.Wait(Constants.TabInitializationDelay);
    
       User32Dll.keybd_event((byte)Keys.F4, 0, (uint)KEYEVENTF.KEYUP, UIntPtr.Zero);
       User32Dll.keybd_event((byte)Keys.Menu, 0, (uint)KEYEVENTF.KEYUP, UIntPtr.Zero);
       User32Dll.keybd_event((byte)Keys.ControlKey, 0, (uint)KEYEVENTF.KEYUP, UIntPtr.Zero);
    
    

     

    This works most of the time, but fails after some browser automation actions like opening/closing few tabs.

    I've tried to set focus to IEFrame, IEServer, TabBandClass windows as well, with similar results.

    I've noticed these entries in the resources of ieframe.dll.mui:

     

    18161, 	"Close &Other Tabs"
    MENUITEM "Close Other &Tabs", 42451, MFT_STRING, MFS_ENABLED
    

     

    I've tried sending these command IDs using WM_COMMAND to above mentioned windows with no luck.

    Here's what I'm yet going to try:

    1. Pass above command IDs to IOleCommandTarget.Exec method;
    2. Access the whole UI tree of Internet Explorer, starting from IEFrame Wnd, through Active Accessibility. Log all accessible objects and found the one for "Close Other Tabs".

    If anyone has better ideas, please let me know..


  • Monday, November 07, 2011 2:13 PM
     
      Has Code

    directUI = FindWindowEx(ieFrame, IntPtr.Zero, "WorkerW", "Navigation Bar");
    
    to
    
    directUI = FindWindowEx(ieFrame, IntPtr.Zero, "WorkerW", null);
    

    Hi!

    Thanks for this great code!

    I need to get the icon that is displayed on the tab. Is this possible?

    Thanks and regards

    Jan


    Edit: The solutions for IE9 from Binaya is not working for me...

    Edit2: I had to change the first line, please see the code.

    • Edited by JanWrage Monday, November 07, 2011 2:45 PM
    • Edited by JanWrage Monday, November 07, 2011 2:59 PM
    •  
  • Tuesday, June 26, 2012 2:17 PM
     
     

    Hi

    Here you have mentioned get URL and Activate the same thing, but how to set URL to the already opened browser with out opening new browser window.i tried with process.start , its always opening new page, i dont want to open new page. i want to open existing browser like i need single instance .

    Thanks in advance

    Thanks

    G.Shankar