Пример #1
0
        /// <summary>
        /// Gets a list of all IE Windows & tabs with the captions of the instances
        /// </summary>
        /// <returns>List with KeyValuePair of WindowDetails and string</returns>
        public static List <KeyValuePair <WindowDetails, string> > GetBrowserTabs()
        {
            var ieHandleList   = new List <IntPtr>();
            var browserWindows = new Dictionary <WindowDetails, List <string> >();

            // Find the IE windows
            foreach (var ieWindow in GetIeWindows())
            {
                try {
                    if (ieHandleList.Contains(ieWindow.Handle))
                    {
                        continue;
                    }
                    if ("IEFrame".Equals(ieWindow.ClassName))
                    {
                        var directUiwd = IEHelper.GetDirectUI(ieWindow);
                        if (directUiwd != null)
                        {
                            var accessible = new Accessible(directUiwd.Handle);
                            browserWindows.Add(ieWindow, accessible.IETabCaptions);
                        }
                    }
                    else if (CoreConfig.WindowClassesToCheckForIE != null && CoreConfig.WindowClassesToCheckForIE.Contains(ieWindow.ClassName))
                    {
                        var singleWindowText = new List <string>();
                        try {
                            var    document2 = GetHtmlDocument(ieWindow);
                            string title     = document2.title;
                            Marshal.ReleaseComObject(document2);
                            if (string.IsNullOrEmpty(title))
                            {
                                singleWindowText.Add(ieWindow.Text);
                            }
                            else
                            {
                                singleWindowText.Add(ieWindow.Text + " - " + title);
                            }
                        } catch {
                            singleWindowText.Add(ieWindow.Text);
                        }
                        browserWindows.Add(ieWindow, singleWindowText);
                    }
                    ieHandleList.Add(ieWindow.Handle);
                } catch (Exception) {
                    Log.Warn("Can't get Info from " + ieWindow.ClassName);
                }
            }

            var returnList = new List <KeyValuePair <WindowDetails, string> >();

            foreach (var windowDetails in browserWindows.Keys)
            {
                foreach (string tab in browserWindows[windowDetails])
                {
                    returnList.Add(new KeyValuePair <WindowDetails, string>(windowDetails, tab));
                }
            }
            return(returnList);
        }
        /// <summary>
        /// Gets a list of all IE Windows & tabs with the captions of the instances
        /// </summary>
        /// <returns>List<KeyValuePair<WindowDetails, string>></returns>
        public static List <KeyValuePair <WindowDetails, string> > GetBrowserTabs()
        {
            List <IntPtr> ieHandleList = new List <IntPtr>();
            Dictionary <WindowDetails, List <string> > browserWindows = new Dictionary <WindowDetails, List <string> >();

            // Find the IE windows
            foreach (WindowDetails ieWindow in GetIEWindows())
            {
                try {
                    if (!ieHandleList.Contains(ieWindow.Handle))
                    {
                        if ("IEFrame".Equals(ieWindow.ClassName))
                        {
                            WindowDetails directUIWD = IEHelper.GetDirectUI(ieWindow);
                            if (directUIWD != null)
                            {
                                Accessible accessible = new Accessible(directUIWD.Handle);
                                browserWindows.Add(ieWindow, accessible.IETabCaptions);
                            }
                        }
                        else if (configuration.WindowClassesToCheckForIE != null && configuration.WindowClassesToCheckForIE.Contains(ieWindow.ClassName))
                        {
                            List <string> singleWindowText = new List <string>();
                            try {
                                IHTMLDocument2 document2 = getHTMLDocument(ieWindow);
                                string         title     = document2.title;
                                Marshal.ReleaseComObject(document2);
                                if (string.IsNullOrEmpty(title))
                                {
                                    singleWindowText.Add(ieWindow.Text);
                                }
                                else
                                {
                                    singleWindowText.Add(ieWindow.Text + " - " + title);
                                }
                            } catch {
                                singleWindowText.Add(ieWindow.Text);
                            }
                            browserWindows.Add(ieWindow, singleWindowText);
                        }
                        ieHandleList.Add(ieWindow.Handle);
                    }
                } catch (Exception) {
                    LOG.Warn("Can't get Info from " + ieWindow.ClassName);
                }
            }

            List <KeyValuePair <WindowDetails, string> > returnList = new List <KeyValuePair <WindowDetails, string> >();

            foreach (WindowDetails windowDetails in browserWindows.Keys)
            {
                foreach (string tab in browserWindows[windowDetails])
                {
                    returnList.Add(new KeyValuePair <WindowDetails, string>(windowDetails, tab));
                }
            }
            return(returnList);
        }
        // Helper method to activate a certain IE Tab
        public static void ActivateIETab(WindowDetails ieWindowDetails, int tabIndex)
        {
            WindowDetails directUIWindowDetails = IEHelper.GetDirectUI(ieWindowDetails);

            // Bring window to the front
            ieWindowDetails.Restore();
            // Get accessible
            Accessible ieAccessible = new Accessible(directUIWindowDetails.Handle);

            // Activate Tab
            ieAccessible.ActivateIETab(tabIndex);
        }
        /// <summary>
        /// Gets a list of all IE Windows with the captions of the open tabs
        /// </summary>
        /// <returns>List<KeyValuePair<WindowDetails, string>></returns>
        public static List <KeyValuePair <WindowDetails, string> > GetTabList()
        {
            List <IntPtr> ieHandleList = new List <IntPtr>();
            Dictionary <WindowDetails, List <string> > browserWindows = new Dictionary <WindowDetails, List <string> >();

            // Find the IE windows
            foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame"))
            {
                try {
                    if (!ieHandleList.Contains(ieWindow.Handle))
                    {
                        WindowDetails directUIWD = IEHelper.GetDirectUI(ieWindow);
                        if (directUIWD != null)
                        {
                            Accessible accessible = new Accessible(directUIWD.Handle);
                            browserWindows.Add(ieWindow, accessible.IETabCaptions);
                        }
                        else
                        {
                            List <string> singleWindowText = new List <string>();
                            singleWindowText.Add(ieWindow.Text);
                            browserWindows.Add(ieWindow, singleWindowText);
                        }
                        ieHandleList.Add(ieWindow.Handle);
                    }
                } catch (Exception) {
                    LOG.Warn("Can't get Info from " + ieWindow.ClassName);
                }
            }

            List <KeyValuePair <WindowDetails, string> > returnList = new List <KeyValuePair <WindowDetails, string> >();

            foreach (WindowDetails windowDetails in browserWindows.Keys)
            {
                foreach (string tab in browserWindows[windowDetails])
                {
                    returnList.Add(new KeyValuePair <WindowDetails, string>(windowDetails, tab));
                }
            }
            return(returnList);
        }
Пример #5
0
        /// <summary>
        /// Helper method which will retrieve the IHTMLDocument2 for the supplied window,
        ///  or return the first if none is supplied.
        /// </summary>
        /// <param name="browserWindow">The WindowDetails to get the IHTMLDocument2 for</param>
        /// <param name="document2">Ref to the IHTMLDocument2 to return</param>
        /// <returns>The WindowDetails to which the IHTMLDocument2 belongs</returns>
        private static DocumentContainer CreateDocumentContainer(WindowDetails browserWindow)
        {
            DocumentContainer returnDocumentContainer = null;
            WindowDetails     returnWindow            = null;
            IHTMLDocument2    returnDocument2         = null;
            // alternative if no match
            WindowDetails  alternativeReturnWindow    = null;
            IHTMLDocument2 alternativeReturnDocument2 = null;

            // Find the IE windows
            foreach (WindowDetails ieWindow in GetIEWindows())
            {
                LOG.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text);

                Accessible    ieAccessible = null;
                WindowDetails directUIWD   = IEHelper.GetDirectUI(ieWindow);
                if (directUIWD != null)
                {
                    ieAccessible = new Accessible(directUIWD.Handle);
                }
                if (ieAccessible == null)
                {
                    if (browserWindow != null)
                    {
                        LOG.InfoFormat("Active Window is {0}", browserWindow.Text);
                    }
                    if (!ieWindow.Equals(browserWindow))
                    {
                        LOG.WarnFormat("No ieAccessible for {0}", ieWindow.Text);
                        continue;
                    }
                    LOG.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text);
                }

                try {
                    // Get the Document
                    IHTMLDocument2 document2 = getHTMLDocument(ieWindow);
                    if (document2 == null)
                    {
                        continue;
                    }

                    // Get the content window handle for the shellWindow.Document
                    IOleWindow oleWindow           = (IOleWindow)document2;
                    IntPtr     contentWindowHandle = IntPtr.Zero;
                    if (oleWindow != null)
                    {
                        oleWindow.GetWindow(out contentWindowHandle);
                    }

                    if (contentWindowHandle != IntPtr.Zero)
                    {
                        // Get the HTMLDocument to check the hasFocus
                        // See: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/60c6c95d-377c-4bf4-860d-390840fce31c/
                        IHTMLDocument4 document4 = (IHTMLDocument4)document2;

                        if (document4.hasFocus())
                        {
                            LOG.DebugFormat("Matched focused document: {0}", document2.title);
                            // Look no further, we got what we wanted!
                            returnDocument2 = document2;
                            returnWindow    = new WindowDetails(contentWindowHandle);
                            break;
                        }
                        try {
                            if (ieWindow.Equals(browserWindow))
                            {
                                returnDocument2 = document2;
                                returnWindow    = new WindowDetails(contentWindowHandle);
                                break;
                            }
                            else if (ieAccessible != null && returnWindow == null && document2.title.Equals(ieAccessible.IEActiveTabCaption))
                            {
                                LOG.DebugFormat("Title: {0}", document2.title);
                                returnDocument2 = document2;
                                returnWindow    = new WindowDetails(contentWindowHandle);
                            }
                            else
                            {
                                alternativeReturnDocument2 = document2;
                                alternativeReturnWindow    = new WindowDetails(contentWindowHandle);
                            }
                        } catch (Exception) {
                            alternativeReturnDocument2 = document2;
                            alternativeReturnWindow    = new WindowDetails(contentWindowHandle);
                        }
                    }
                } catch (Exception e) {
                    LOG.ErrorFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text);
                    LOG.Error(e);
                }
            }

            // check if we have something to return
            if (returnWindow != null)
            {
                // As it doesn't have focus, make sure it's active
                returnWindow.Restore();
                returnWindow.GetParent();

                // Create the container
                try {
                    returnDocumentContainer = new DocumentContainer(returnDocument2, returnWindow);
                } catch (Exception e) {
                    LOG.Error("Major problem: Problem retrieving Document.");
                    LOG.Error(e);
                }
            }

            if (returnDocumentContainer == null && alternativeReturnDocument2 != null)
            {
                // As it doesn't have focus, make sure it's active
                alternativeReturnWindow.Restore();
                alternativeReturnWindow.GetParent();
                // Create the container
                try {
                    returnDocumentContainer = new DocumentContainer(alternativeReturnDocument2, alternativeReturnWindow);
                } catch (Exception e) {
                    LOG.Error("Major problem: Problem retrieving Document.");
                    LOG.Error(e);
                }
            }
            return(returnDocumentContainer);
        }
        /// <summary>
        /// Helper method which will retrieve the IHTMLDocument2 for the supplied window,
        ///  or return the first if none is supplied.
        /// </summary>
        /// <param name="browserWindowDetails">The WindowDetails to get the IHTMLDocument2 for</param>
        /// <param name="document2">Ref to the IHTMLDocument2 to return</param>
        /// <returns>The WindowDetails to which the IHTMLDocument2 belongs</returns>
        private static DocumentContainer GetDocument(WindowDetails activeWindow)
        {
            DocumentContainer returnDocumentContainer = null;
            WindowDetails     returnWindow            = null;
            IHTMLDocument2    returnDocument2         = null;
            // alternative if no match
            WindowDetails  alternativeReturnWindow    = null;
            IHTMLDocument2 alternativeReturnDocument2 = null;

            // Find the IE window
            foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame"))
            {
                LOG.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text);

                Accessible    ieAccessible = null;
                WindowDetails directUIWD   = IEHelper.GetDirectUI(ieWindow);
                if (directUIWD != null)
                {
                    ieAccessible = new Accessible(directUIWD.Handle);
                }
                if (ieAccessible == null)
                {
                    LOG.InfoFormat("Active Window is {0}", activeWindow.Text);
                    if (!ieWindow.Equals(activeWindow))
                    {
                        LOG.WarnFormat("No ieAccessible for {0}", ieWindow.Text);
                        continue;
                    }
                    LOG.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text);
                }

                try {
                    // Get the Document
                    IHTMLDocument2 document2     = null;
                    uint           windowMessage = User32.RegisterWindowMessage("WM_HTML_GETOBJECT");
                    if (windowMessage == 0)
                    {
                        LOG.WarnFormat("Couldn't register WM_HTML_GETOBJECT");
                        continue;
                    }

                    WindowDetails ieServer = ieWindow.GetChild("Internet Explorer_Server");
                    if (ieServer == null)
                    {
                        LOG.WarnFormat("No Internet Explorer_Server for {0}", ieWindow.Text);
                        continue;
                    }
                    LOG.DebugFormat("Trying WM_HTML_GETOBJECT on {0}", ieServer.ClassName);
                    UIntPtr response;
                    User32.SendMessageTimeout(ieServer.Handle, windowMessage, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 1000, out response);
                    if (response != UIntPtr.Zero)
                    {
                        document2 = (IHTMLDocument2)Accessible.ObjectFromLresult(response, typeof(IHTMLDocument).GUID, IntPtr.Zero);
                        if (document2 == null)
                        {
                            LOG.Error("No IHTMLDocument2 found");
                            continue;
                        }
                    }
                    else
                    {
                        LOG.Error("No answer on WM_HTML_GETOBJECT.");
                        continue;
                    }

                    // Get the content window handle for the shellWindow.Document
                    IOleWindow oleWindow           = (IOleWindow)document2;
                    IntPtr     contentWindowHandle = IntPtr.Zero;
                    if (oleWindow != null)
                    {
                        oleWindow.GetWindow(out contentWindowHandle);
                    }

                    if (contentWindowHandle != IntPtr.Zero)
                    {
                        // Get the HTMLDocument to check the hasFocus
                        // See: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/60c6c95d-377c-4bf4-860d-390840fce31c/
                        IHTMLDocument4 document4 = (IHTMLDocument4)document2;

                        if (document4.hasFocus())
                        {
                            LOG.DebugFormat("Matched focused document: {0}", document2.title);
                            // Look no further, we got what we wanted!
                            returnDocument2 = document2;
                            returnWindow    = new WindowDetails(contentWindowHandle);
                            break;
                        }
                        try {
                            if (ieWindow.Equals(activeWindow))
                            {
                                returnDocument2 = document2;
                                returnWindow    = new WindowDetails(contentWindowHandle);
                                break;
                            }
                            else if (ieAccessible != null && returnWindow == null && document2.title.Equals(ieAccessible.IEActiveTabCaption))
                            {
                                LOG.DebugFormat("Title: {0}", document2.title);
                                returnDocument2 = document2;
                                returnWindow    = new WindowDetails(contentWindowHandle);
                            }
                            else
                            {
                                alternativeReturnDocument2 = document2;
                                alternativeReturnWindow    = new WindowDetails(contentWindowHandle);
                            }
                        } catch (Exception) {
                            alternativeReturnDocument2 = document2;
                            alternativeReturnWindow    = new WindowDetails(contentWindowHandle);
                        }
                    }
                } catch (Exception e) {
                    LOG.Error(e);
                    LOG.DebugFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text);
                }
            }

            // check if we have something to return
            if (returnWindow != null)
            {
                // As it doesn't have focus, make sure it's active
                returnWindow.Restore();
                returnWindow.GetParent();

                // Create the container
                returnDocumentContainer = new DocumentContainer(returnDocument2, returnWindow);
            }

            if (returnDocumentContainer == null && alternativeReturnDocument2 != null)
            {
                // As it doesn't have focus, make sure it's active
                alternativeReturnWindow.Restore();
                alternativeReturnWindow.GetParent();
                // Create the container
                returnDocumentContainer = new DocumentContainer(alternativeReturnDocument2, alternativeReturnWindow);
            }
            return(returnDocumentContainer);
        }