Ejemplo n.º 1
0
        /// <summary>
        /// Gets all windows for the process.
        /// </summary>
        /// <param name="process"> The process to get windows for. </param>
        /// <param name="application"> The application the elements are for. </param>
        /// <returns> The array of windows. </returns>
        public static IEnumerable<Window> GetWindows(this Process process, Application application)
        {
            // There is a issue in Windows 10 and Cortana (or modern apps) where there is a 60 second delay when walking the root element.
            // When you hit the last sibling it delays. For now we are simply going to return the main window and we'll roll this code
            // back once the Windows 10 issue has been resolved.

            process.Refresh();
            var response = new List<Window>();
            var automation = new CUIAutomationClass();

            foreach (var handle in EnumerateProcessWindowHandles(process.Id))
            {
                var automationElement = automation.ElementFromHandle(handle);
                var element = Element.Create(automationElement, application, null) as Window;
                if (element != null)
                {
                    response.Add(element);
                }
            }

            return response;
        }
Ejemplo n.º 2
0
        private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
        {
            var handles = new List<IntPtr>();

            foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
            {
                NativeMethods.EnumThreadWindows(thread.Id, (hWnd, lParam) =>
                {
                    handles.Add(hWnd);
                    return true;
                }, IntPtr.Zero);
            }

            return handles;
        }