Exemplo n.º 1
0
        /// <summary>
        ///     Enumerate the windows and child handles (IntPtr) via an Observable
        /// </summary>
        /// <param name="hWndParent">IntPtr with the hwnd of the parent, or null for all</param>
        /// <returns>IObservable with IntPtr</returns>
        public static IObservable <IntPtr> EnumerateWindowHandlesAsync(IntPtr?hWndParent = null)
        {
            return(Observable.Create <IntPtr>(observer =>
            {
                var cancellationTokenSource = new CancellationTokenSource();
                Task.Run(() =>
                {
                    bool EnumWindowsProc(IntPtr hwnd, IntPtr param)
                    {
                        // check if we should continue
                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            return false;
                        }

                        observer.OnNext(hwnd);
                        return !cancellationTokenSource.IsCancellationRequested;
                    }

                    User32Api.EnumChildWindows(hWndParent ?? IntPtr.Zero, EnumWindowsProc, IntPtr.Zero);
                    observer.OnCompleted();
                }, cancellationTokenSource.Token);
                return new CancellationDisposable(cancellationTokenSource);
            }));
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Enumerate the windows / child windows via an Observable
 /// </summary>
 /// <param name="hWndParent">IntPtr with the hwnd of the parent, or null for all</param>
 /// <param name="cancellationToken">CancellationToken</param>
 /// <returns>IObservable with WinWindowInfo</returns>
 public static IObservable <IInteropWindow> EnumerateWindowsAsync(IntPtr?hWndParent = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Observable.Create <IInteropWindow>(observer =>
     {
         var continueWithEnumeration = true;
         Task.Run(() =>
         {
             User32Api.EnumChildWindows(hWndParent ?? IntPtr.Zero, (hwnd, param) =>
             {
                 // check if we should continue
                 if (cancellationToken.IsCancellationRequested || !continueWithEnumeration)
                 {
                     return false;
                 }
                 var windowInfo = InteropWindowFactory.CreateFor(hwnd);
                 observer.OnNext(windowInfo);
                 return continueWithEnumeration;
             }, IntPtr.Zero);
             observer.OnCompleted();
         }, cancellationToken);
         return () =>
         {
             // Stop enumerating
             continueWithEnumeration = false;
         };
     }));
 }
Exemplo n.º 3
0
        public IEnumerable <IWindow> EnumerateWindows()
        {
            var windows = new List <IWindow>();

            User32Api.EnumChildWindows(IntPtr.Zero, (hWnd, _) =>
            {
                windows.Add(new Win32Window(hWnd));

                return(true);
            }, IntPtr.Zero);

            return(windows);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Enumerate the windows / child windows (this is NOT lazy)
        /// </summary>
        /// <param name="parent">IInteropWindow with the hwnd of the parent, or null for all</param>
        /// <param name="wherePredicate">Func for the where</param>
        /// <param name="takeWhileFunc">Func which can decide to stop enumerating</param>
        /// <returns>IEnumerable with InteropWindow</returns>
        public static IEnumerable <IInteropWindow> EnumerateWindows(IInteropWindow parent = null, Func <IInteropWindow, bool> wherePredicate = null, Func <IInteropWindow, bool> takeWhileFunc = null)
        {
            var result = new List <IInteropWindow>();

            User32Api.EnumChildWindows(parent?.Handle ?? IntPtr.Zero, (hwnd, param) =>
            {
                // check if we should continue
                var windowInfo = InteropWindowFactory.CreateFor(hwnd);

                if (wherePredicate?.Invoke(windowInfo) != false)
                {
                    result.Add(windowInfo);
                }
                return(takeWhileFunc?.Invoke(windowInfo) != false);
            }, IntPtr.Zero);
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Enumerate the windows / child windows (this is NOT lazy)
        /// </summary>
        /// <param name="parent">IInteropWindow with the hwnd of the parent, or null for all</param>
        /// <param name="wherePredicate">Func for the where</param>
        /// <param name="takeWhileFunc">Func which can decide to stop enumerating, the second argument is the current count</param>
        /// <returns>IEnumerable with InteropWindow</returns>
        public static IEnumerable <IInteropWindow> EnumerateWindows(IInteropWindow parent = null, Func <IInteropWindow, bool> wherePredicate = null, Func <IInteropWindow, int, bool> takeWhileFunc = null)
        {
            var result = new List <IInteropWindow>();

            bool EnumWindowsProc(IntPtr hwnd, IntPtr param)
            {
                // check if we should continue
                var interopWindow = InteropWindowFactory.CreateFor(hwnd);

                if (wherePredicate == null || wherePredicate(interopWindow))
                {
                    result.Add(interopWindow);
                }

                return(takeWhileFunc == null || takeWhileFunc(interopWindow, result.Count));
            }

            User32Api.EnumChildWindows(parent?.Handle ?? IntPtr.Zero, EnumWindowsProc, IntPtr.Zero);
            return(result);
        }