Esempio n. 1
0
        public BackupAndRestorePosition(IObservable <EventType> events, PositionWindowsByProcessCollector collector, PostionRestoreOperator restoreOperator, IReadOnlyCollection <string> excludedProcesses, INotificationBus bus)
        {
            _collector = collector;

            var options = new PositionWindowsByProcessCollectorOptions(
                window => string.IsNullOrWhiteSpace(window.Title)
                //|| window.Rectangle.IsEmpty
                //|| window.Rectangle.IsOffScreen() since there is a monitor left to main window, this does not work
                || window.Placement.showCmd == User32.ShowState.SW_HIDE ||
                !window.IsVisible
                ,
                process => excludedProcesses.Any(n => process.ProcessName.Contains(n))
                );

            _subscription = events
                            .Do(e =>
            {
                if (e == EventType.Backup)
                {
                    _positionWindowsByProcesses = _collector.Collect(options);
                    bus.Emit(new BackupNotification(_positionWindowsByProcesses));
                    Console.WriteLine($"{DateTime.Now} - BACKUP");
                }
                else
                {
                    restoreOperator.Restore(_positionWindowsByProcesses);
                    Console.WriteLine($"{DateTime.Now} - RESTORE");
                }
            })
                            .Subscribe();
        }
Esempio n. 2
0
        public IReadOnlyCollection <PositionWindowsByProcess> Collect(PositionWindowsByProcessCollectorOptions options)
        {
            return(Process.GetProcesses()
                   .Where(p => !options.ExcludeProcess(p))
                   .Select(p =>
            {
                var handles = GetRootWindowsOfProcess(p.Id);
                var windows = handles
                              .Select(hWnd =>
                {
                    var rect = User32.GetWindowRectFrom(hWnd);
                    var title = User32.GetWindowTitle(hWnd);
                    var placement = User32.GetWindowPlacement(hWnd);
                    var isVisiable = User32.IsWindowVisible(hWnd);

                    return new PositionedWindow(rect, hWnd, title, placement, isVisiable);
                })
                              .Where(w => !options.ExcludeWindow(w))
                              .ToArray();
                return new PositionWindowsByProcess(p, windows);
            })
                   .Where(pw => pw.RootWindows.Count > 0)
                   .ToArray());
        }