Пример #1
0
        public RegionSelectorWindow(
            [NotNull] IFactory <IRegionSelectorViewModel, ICloseController <RegionSelectorResult> > viewModelFactory)
        {
            InitializeComponent();

            Disposable.Create(() => Log.Debug("RegionSelectorWindow disposed")).AddTo(anchors);
            Closed += OnClosed;
            Loaded += OnLoaded;

            CloseController = new ParameterizedCloseController <Window, RegionSelectorResult>(this,
                                                                                              result =>
            {
                var windowHandle = new WindowInteropHelper(this).Handle;
                var state        = new { IsVisible, Visibility, IsActive };
                if (state.IsVisible && state.Visibility == Visibility.Visible)
                {
                    Log.Debug($"[{windowHandle.ToHexadecimal()}] Closing RegionSelector window({state}), result - {result.DumpToTextRaw()}");
                    Result = result;
                    Close();
                }
                else
                {
                    Log.Debug($"[{windowHandle.ToHexadecimal()}] Ignoring Close request for RegionSelector window({state}) - already closed");
                }
            });

            viewModel   = viewModelFactory.Create(CloseController).AddTo(anchors);
            DataContext = viewModel;
        }
Пример #2
0
            private void OnLoaded(object sender, RoutedEventArgs e)
            {
                this.Loaded -= OnLoaded;

                Log.Info("HookForm loaded, applying style...");
                var hwnd = new WindowInteropHelper(this).EnsureHandle();

                Log.Debug($"HookForm handle: {hwnd.ToHexadecimal()}");
                UnsafeNative.HideSystemMenu(hwnd);
                UnsafeNative.SetWindowExTransparent(hwnd);
                UnsafeNative.SetWindowRgn(hwnd, Rectangle.Empty);
                Log.Info("HookForm successfully initialized");
            }
Пример #3
0
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var windowHandle = new WindowInteropHelper(this).Handle;

            Log.Debug($"[{windowHandle.ToHexadecimal()}] Window loaded");

            var screen      = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Control.MousePosition);
            var workingArea = screen.WorkingArea;

            Left        = workingArea.Left;
            Top         = workingArea.Top;
            Width       = workingArea.Width;
            Height      = workingArea.Height;
            WindowState = WindowState.Maximized;

            if (!UnsafeNative.SetForegroundWindow(windowHandle))
            {
                Log.Warn($"[{windowHandle.ToHexadecimal()}] Failed to bring window to front");
            }
            var closeWindowSink = new Subject <string>();

            Disposable.Create(() => closeWindowSink.OnNext("window is disposed"))
            .AddTo(anchors);

            Observable.Merge(
                Observable.FromEventPattern <KeyEventHandler, KeyEventArgs>(h => PreviewKeyDown += h, h => PreviewKeyDown -= h)
                .Where(x => x.EventArgs.Key == Key.Escape)
                .Select(x => "ESC pressed"),
                Observable.FromEventPattern <RoutedEventHandler, RoutedEventArgs>(h => LostFocus += h, h => LostFocus -= h).Select(x => "window LostFocus"),
                Observable.FromEventPattern <EventHandler, EventArgs>(h => Deactivated           += h, h => Deactivated -= h).Select(x => "window Deactivated"))
            .Subscribe(closeWindowSink)
            .AddTo(anchors);

            closeWindowSink
            .Take(1)
            .Subscribe(reason => CloseController.Close(new RegionSelectorResult()
            {
                Reason = reason
            }))
            .AddTo(anchors);

            viewModel.SelectWindow()
            .Subscribe(
                result =>
            {
                CloseController.Close(result);
            },
                Log.HandleException,
                () => { closeWindowSink.OnNext("region selection cancelled"); })
            .AddTo(anchors);

            viewModel.WhenAnyValue(x => x.SelectionCandidate)
            .Subscribe(
                regionResult =>
            {
                if (regionResult == null || !regionResult.IsValid)
                {
                    RegionCandidate.Visibility = Visibility.Collapsed;
                    return;
                }

                RegionCandidate.Visibility = Visibility.Visible;

                var bounds = regionResult.Window.WindowBounds.ScaleToWpf();
                Canvas.SetLeft(RegionCandidate, bounds.X);
                Canvas.SetTop(RegionCandidate, bounds.Y);
                RegionCandidate.Width  = bounds.Width;
                RegionCandidate.Height = bounds.Height;
            })
            .AddTo(anchors);
        }