예제 #1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            IntPtr loginWnd = User32Api.FindWindow(null, "Login");

            if (loginWnd != IntPtr.Zero)
            {
                User32Api.SetForegroundWindow(loginWnd);
                Current.Shutdown();
                return;
            }

            IntPtr parenthWnd = User32Api.FindWindow(null, "密云直播云课堂");

            if (parenthWnd != IntPtr.Zero)
            {
                User32Api.SetForegroundWindow(parenthWnd);
                Current.Shutdown();
                return;
            }

            base.OnStartup(e);
            string startupPatams = string.Empty;

            if (e.Args.Length > 0)
            {
                startupPatams = e.Args[0];
            }
            Bootstrapper bootstrapper = new Bootstrapper(startupPatams);

            bootstrapper.Run();
        }
예제 #2
0
        /// <summary>
        ///     Set the window as foreground window
        /// </summary>
        /// <param name="interopWindow">The window to bring to the foreground</param>
        /// <param name="workaround">bool with true to use a trick (press Alt) to really bring the window to the foreground</param>
        public static async ValueTask ToForegroundAsync(this IInteropWindow interopWindow, bool workaround = true)
        {
            // Nothing we can do if it's not visible!
            if (!interopWindow.IsVisible())
            {
                return;
            }
            // Window is already the foreground window
            if (User32Api.GetForegroundWindow() == interopWindow.Handle)
            {
                return;
            }
            if (interopWindow.IsMinimized())
            {
                interopWindow.Restore();
                while (interopWindow.IsMinimized())
                {
                    await Task.Delay(50).ConfigureAwait(false);
                }
            }

            // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx
            if (workaround)
            {
                // Simulate an "ALT" key press, make it double to remove menu activation
                KeyboardInputGenerator.KeyPresses(VirtualKeyCode.Menu, VirtualKeyCode.Menu);
            }
            // Show window in foreground.
            User32Api.BringWindowToTop(interopWindow.Handle);
            User32Api.SetForegroundWindow(interopWindow.Handle);
        }
예제 #3
0
 /// <summary>
 /// 激活窗体
 /// </summary>
 /// <returns></returns>
 public bool SetAction()
 {
     if (!this)
     {
         return(false);
     }
     return(User32Api.SetForegroundWindow(IntPtr));
 }
예제 #4
0
        private void OpenWindow <T>(string title) where T : MetroWindow
        {
            IntPtr ptr = GetWindowHandle(title);

            if (ptr == IntPtr.Zero)
            {
                ShowDialogWindow <T>();
            }
            else
            {
                User32Api.SetForegroundWindow(ptr);
            }
        }
예제 #5
0
        /// <summary>
        ///     Set the window as foreground window
        /// </summary>
        /// <param name="interopWindow">The window to bring to the foreground</param>
        public static async ValueTask ToForegroundAsync(this IInteropWindow interopWindow)
        {
            // Nothing we can do if it's not visible!
            if (!interopWindow.IsVisible())
            {
                return;
            }

            var foregroundWindow = User32Api.GetForegroundWindow();

            // Window is already the foreground window
            if (foregroundWindow == interopWindow.Handle)
            {
                return;
            }
            if (interopWindow.IsMinimized())
            {
                interopWindow.Restore();
                while (interopWindow.IsMinimized())
                {
                    await Task.Delay(50).ConfigureAwait(false);
                }
            }

            // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx
            // It was advised to use the menu (ALT) key, but this was a solution which Jan Karger showed me

            var threadId1 = User32Api.GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
            var threadId2 = User32Api.GetWindowThreadProcessId(interopWindow.Handle, IntPtr.Zero);

            // Show window in foreground.
            if (threadId1 != threadId2)
            {
                User32Api.AttachThreadInput(threadId1, threadId2, 1);
                User32Api.SetForegroundWindow(interopWindow.Handle);
                User32Api.AttachThreadInput(threadId1, threadId2, 0);
            }
            else
            {
                User32Api.SetForegroundWindow(interopWindow.Handle);
            }

            // Show window in foreground.
            User32Api.BringWindowToTop(interopWindow.Handle);
            User32Api.SetForegroundWindow(interopWindow.Handle);
        }
예제 #6
0
        /// <summary>
        ///     Test scrolling a window
        /// </summary>
        /// <returns></returns>
        //[StaFact]
        private async Task TestScrollingAsync()
        {
            var breakScroll = false;

            IDisposable keyboardhook = null;

            try
            {
                keyboardhook = KeyboardHook.KeyboardEvents.Where(args => args.Key == VirtualKeyCode.Escape).Subscribe(args => breakScroll = true);
                // Start a process to test against
                using (var process = Process.Start("notepad.exe", "C:\\Windows\\setupact.log"))
                {
                    // Make sure it's started
                    Assert.NotNull(process);
                    // Wait until the process started it's message pump (listening for input)
                    process.WaitForInputIdle();

                    try
                    {
                        // Find the belonging window, by the process id
                        var notepadWindow = WindowsEnumerator.EnumerateWindows()
                                            .FirstOrDefault(interopWindow =>
                        {
                            User32Api.GetWindowThreadProcessId(interopWindow.Handle, out var processId);
                            return(processId == process.Id);
                        });
                        Assert.NotNull(notepadWindow);

                        // Create a WindowScroller
                        var scroller = notepadWindow.GetChildren().Select(window => window.GetWindowScroller()).FirstOrDefault();

                        Assert.NotNull(scroller);
                        // Notepad should have ScrollBarInfo
                        scroller.GetScrollbarInfo();
                        Assert.True(scroller.ScrollBar.HasValue);

                        Log.Info().WriteLine("Scrollbar info: {0}", scroller.ScrollBar.Value);

                        User32Api.SetForegroundWindow(scroller.ScrollingWindow.Handle);
                        await Task.Delay(1000);

                        // Just make sure the window is changed
                        KeyboardInputGenerator.KeyPresses(VirtualKeyCode.Next, VirtualKeyCode.Down);
                        await Task.Delay(2000);

                        scroller.ScrollMode  = ScrollModes.WindowsMessage;
                        scroller.ShowChanges = false;
                        // Move the window to the start
                        Assert.True(scroller.Start());
                        // A delay to make the window move
                        await Task.Delay(2000);

                        // Check if it did move to the start
                        Assert.True(scroller.IsAtStart);

                        // Loop
                        do
                        {
                            if (breakScroll)
                            {
                                break;
                            }
                            // Next "page"
                            Assert.True(scroller.Next());
                            // Wait a bit, so the window can update
                            await Task.Delay(300);

                            // Loop as long as we are not at the end yet
                        } while (!scroller.IsAtEnd);
                        scroller.Reset();
                    }
                    finally
                    {
                        // Kill the process
                        process.Kill();
                    }
                }
            }
            finally
            {
                keyboardhook?.Dispose();
            }
        }
예제 #7
0
        /// <summary>
        /// Start the capture
        /// </summary>
        /// <returns>Bitmap</returns>
        public Bitmap Capture()
        {
            if (_windowScroller.NeedsFocus())
            {
                User32Api.SetForegroundWindow(_windowScroller.ScrollBarWindow.Handle);
                Application.DoEvents();
                Thread.Sleep(Delay);
                Application.DoEvents();
            }

            // Find the area which is scrolling

            // 1. Take the client bounds
            var clientBounds = _windowScroller.ScrollBarWindow.GetInfo().ClientBounds;

            // Use a region for steps 2 and 3
            using (var region = new Region(clientBounds))
            {
                // 2. exclude the children, if any
                foreach (var interopWindow in _windowScroller.ScrollBarWindow.GetChildren())
                {
                    region.Exclude(interopWindow.GetInfo().Bounds);
                }
                // 3. exclude the scrollbar, if it can be found
                if (_windowScroller.ScrollBar.HasValue)
                {
                    region.Exclude(_windowScroller.ScrollBar.Value.Bounds);
                }
                // Get the bounds of the region
                using (var screenGraphics = Graphics.FromHwnd(User32Api.GetDesktopWindow()))
                {
                    var rectangleF = region.GetBounds(screenGraphics);
                    clientBounds = new NativeRect((int)rectangleF.X, (int)rectangleF.Y, (int)rectangleF.Width, (int)rectangleF.Height);
                }
            }

            if (clientBounds.Width * clientBounds.Height <= 0)
            {
                return(null);
            }
            // Move the window to the start
            _windowScroller.Start();

            // Register a keyboard hook to make it possible to ESC the capturing
            var breakScroll  = false;
            var keyboardHook = KeyboardHook.KeyboardEvents
                               .Where(args => args.Key == VirtualKeyCodes.ESCAPE)
                               .Subscribe(args =>
            {
                args.Handled = true;
                breakScroll  = true;
            });
            Bitmap resultImage = null;

            try
            {
                // A delay to make the window move
                Application.DoEvents();
                Thread.Sleep(Delay);
                Application.DoEvents();

                if (_windowScroller.IsAtStart)
                {
                    using (var bitmapStitcher = new BitmapStitcher())
                    {
                        bitmapStitcher.AddBitmap(WindowCapture.CaptureRectangle(clientBounds));

                        // Loop as long as we are not at the end yet
                        while (!_windowScroller.IsAtEnd && !breakScroll)
                        {
                            // Next "page"
                            _windowScroller.Next();
                            // Wait a bit, so the window can update
                            Application.DoEvents();
                            Thread.Sleep(Delay);
                            Application.DoEvents();
                            // Capture inside loop
                            bitmapStitcher.AddBitmap(WindowCapture.CaptureRectangle(clientBounds));
                        }
                        resultImage = bitmapStitcher.Result();
                    }
                }
                else
                {
                    resultImage = WindowCapture.CaptureRectangle(clientBounds);
                }
            }
            catch (Exception ex)
            {
                Log.Error().WriteLine(ex);
            }
            finally
            {
                // Remove hook for escape
                keyboardHook.Dispose();
                // Try to reset location
                _windowScroller.Reset();
            }

            return(resultImage);
        }
예제 #8
0
 /// <summary>
 ///     Make sure the form is visible
 /// </summary>
 /// <param name="sender">sender object</param>
 /// <param name="e">EventArgs</param>
 private void OAuthLoginForm_Load(object sender, EventArgs e)
 {
     Visible = true;
     User32Api.SetForegroundWindow(Handle);
 }