/// <summary>
        /// Return an IEnumerable with the currently opened IE urls
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <string> GetIEUrls()
        {
            List <string> urls = new List <string>();

            // Find the IE window
            foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame"))
            {
                WindowDetails directUIWD = GetDirectUI(ieWindow);
                if (directUIWD != null)
                {
                    Accessible    ieAccessible = new Accessible(directUIWD.Handle);
                    List <string> ieUrls       = ieAccessible.IETabUrls;
                    if (ieUrls != null && ieUrls.Count > 0)
                    {
                        foreach (string url in ieUrls)
                        {
                            if (!urls.Contains(url))
                            {
                                urls.Add(url);
                            }
                        }
                    }
                }
            }

            return(urls);
        }
예제 #2
0
 /// <summary>
 /// Simple check if IE is running
 /// </summary>
 /// <returns>bool</returns>
 public static bool IsIERunning()
 {
     foreach (WindowDetails shellWindow in WindowDetails.GetAllWindows("IEFrame"))
     {
         return(true);
     }
     return(false);
 }
예제 #3
0
        public static IEnumerable <string> GetBrowserUrls()
        {
            // FireFox
            foreach (WindowDetails window in WindowDetails.GetAllWindows("MozillaWindowClass"))
            {
                if (window.Text.Length == 0)
                {
                    continue;
                }
                AutomationElement currentElement  = AutomationElement.FromHandle(window.Handle);
                Condition         conditionCustom = new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom), new PropertyCondition(AutomationElement.IsOffscreenProperty, false));
                for (int i = 5; i > 0 && currentElement != null; i--)
                {
                    currentElement = currentElement.FindFirst(TreeScope.Children, conditionCustom);
                }
                if (currentElement == null)
                {
                    continue;
                }

                Condition         conditionDocument = new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document), new PropertyCondition(AutomationElement.IsOffscreenProperty, false));
                AutomationElement docElement        = currentElement.FindFirst(TreeScope.Children, conditionDocument);
                if (docElement == null)
                {
                    continue;
                }
                foreach (AutomationPattern pattern in docElement.GetSupportedPatterns())
                {
                    if (pattern.ProgrammaticName != "ValuePatternIdentifiers.Pattern")
                    {
                        continue;
                    }
                    string url = (docElement.GetCurrentPattern(pattern) as ValuePattern).Current.Value.ToString();
                    if (!string.IsNullOrEmpty(url))
                    {
                        yield return(url);

                        break;
                    }
                }
            }

            foreach (WindowDetails window in WindowDetails.GetAllWindows("MozillaWindowClass"))
            {
                if (window.Text.Length == 0)
                {
                    continue;
                }
                AutomationElement currentElement = AutomationElement.FromHandle(window.Handle);
            }
            foreach (string url in IEHelper.GetIEUrls())
            {
                yield return(url);
            }
        }
예제 #4
0
 /// <summary>
 /// Return an IEnumerable with the currently opened IE urls
 /// </summary>
 /// <returns></returns>
 public static IEnumerable <string> GetIEUrls()
 {
     // Find the IE window
     foreach (WindowDetails ieWindow in WindowDetails.GetAllWindows("IEFrame"))
     {
         WindowDetails directUIWD = GetDirectUI(ieWindow);
         if (directUIWD != null)
         {
             Accessible ieAccessible = new Accessible(directUIWD.Handle);
             foreach (string url in ieAccessible.IETabUrls)
             {
                 yield return(url);
             }
         }
     }
 }
예제 #5
0
 /// <summary>
 /// Get Windows displaying an IE
 /// </summary>
 /// <returns>List<WindowDetails></returns>
 public static IEnumerable <WindowDetails> GetIEWindows()
 {
     foreach (WindowDetails possibleIEWindow in WindowDetails.GetAllWindows())
     {
         if (possibleIEWindow.Text.Length == 0)
         {
             continue;
         }
         if (possibleIEWindow.ClientRectangle.IsEmpty)
         {
             continue;
         }
         if (IsIEWindow(possibleIEWindow))
         {
             yield return(possibleIEWindow);
         }
     }
 }
예제 #6
0
        /// <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 = 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);
        }
예제 #7
0
파일: CopyData.cs 프로젝트: zhk/greenshot
        /// <summary>
        /// Sends the specified object on this channel to any other
        /// applications which are listening.  The object must have the
        /// SerializableAttribute set, or must implement ISerializable.
        /// </summary>
        /// <param name="obj">The object to send</param>
        /// <returns>The number of recipients</returns>
        public int Send(object obj)
        {
            int recipients = 0;

            if (_recreateChannel)
            {
                // handle has changed
                AddChannel();
            }

            CopyDataObjectData cdo = new CopyDataObjectData(obj, ChannelName);


            // Try to do a binary serialization on obj.
            // This will throw and exception if the object to
            // be passed isn't serializable.
            BinaryFormatter b      = new BinaryFormatter();
            MemoryStream    stream = new MemoryStream();

            b.Serialize(stream, cdo);
            stream.Flush();

            // Now move the data into a pointer so we can send
            // it using WM_COPYDATA:
            // Get the length of the data:
            int dataSize = (int)stream.Length;

            if (dataSize > 0)
            {
                // This isn't very efficient if your data is very large.
                // First we copy to a byte array, then copy to a CoTask
                // Mem object... And when we use WM_COPYDATA windows will
                // make yet another copy!  But if you're talking about 4K
                // or less of data then it doesn't really matter.
                byte[] data = new byte[dataSize];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(data, 0, dataSize);
                IntPtr ptrData = Marshal.AllocCoTaskMem(dataSize);
                Marshal.Copy(data, 0, ptrData, dataSize);

                // Send the data to each window identified on
                // the channel:
                foreach (WindowDetails window in WindowDetails.GetAllWindows())
                {
                    if (!window.Handle.Equals(_owner.Handle))
                    {
                        if (GetProp(window.Handle, ChannelName) != IntPtr.Zero)
                        {
                            COPYDATASTRUCT cds = new COPYDATASTRUCT
                            {
                                cbData = dataSize,
                                dwData = IntPtr.Zero,
                                lpData = ptrData
                            };
                            SendMessage(window.Handle, WM_COPYDATA, _owner.Handle, ref cds);
                            recipients += Marshal.GetLastWin32Error() == 0 ? 1 : 0;
                        }
                    }
                }

                // Clear up the data:
                Marshal.FreeCoTaskMem(ptrData);
            }
            stream.Close();

            return(recipients);
        }
예제 #8
0
        /// <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   = 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);
        }
        /// <summary>
        /// Pre-Initialization for CaptureWithFeedback, this will get all the windows before we change anything
        /// </summary>
        private Thread PrepareForCaptureWithFeedback()
        {
            windows = new List <WindowDetails>();

            Thread getWindowDetailsThread = new Thread(delegate()
            {
                // Start Enumeration of "active" windows
                foreach (WindowDetails window in WindowDetails.GetAllWindows())
                {
                    // Window should be visible and not ourselves
                    if (!window.Visible)
                    {
                        continue;
                    }

                    // Skip empty
                    Rectangle windowRectangle = window.WindowRectangle;
                    Size windowSize           = windowRectangle.Size;
                    if (windowSize.Width == 0 || windowSize.Height == 0)
                    {
                        continue;
                    }

                    // Make sure the details are retrieved once
                    window.FreezeDetails();

                    // Force children retrieval, sometimes windows close on losing focus and this is solved by caching
                    int goLevelDeep = 3;
                    if (conf.WindowCaptureAllChildLocations)
                    {
                        goLevelDeep = 20;
                    }
                    window.GetChildren(goLevelDeep);
                    lock (windows)
                    {
                        windows.Add(window);
                    }

                    // Get window rectangle as capture Element
                    CaptureElement windowCaptureElement = new CaptureElement(windowRectangle);
                    if (capture == null)
                    {
                        break;
                    }
                    capture.Elements.Add(windowCaptureElement);

                    if (!window.HasParent)
                    {
                        // Get window client rectangle as capture Element, place all the other "children" in there
                        Rectangle clientRectangle = window.ClientRectangle;
                        CaptureElement windowClientCaptureElement = new CaptureElement(clientRectangle);
                        windowCaptureElement.Children.Add(windowClientCaptureElement);
                        AddCaptureElementsForWindow(windowClientCaptureElement, window, goLevelDeep);
                    }
                    else
                    {
                        AddCaptureElementsForWindow(windowCaptureElement, window, goLevelDeep);
                    }
                }
                lock (windows)
                {
                    windows = WindowDetails.SortByZOrder(IntPtr.Zero, windows);
                }
            });

            getWindowDetailsThread.Name         = "Retrieve window details";
            getWindowDetailsThread.IsBackground = true;
            getWindowDetailsThread.Start();
            return(getWindowDetailsThread);
        }