Пример #1
0
        /*
         * /// <summary>
         * /// 鼠标按压
         * /// </summary>
         * /// <param name="offsetX">offsetX</param>
         * /// <param name="offsetY">offsetY</param>
         * /// <param name="rightButton">右键</param>
         * /// <param name="pressedMillionSeconds">按压时长</param>
         * /// <param name="cancellationToken">cancellationToken</param>
         * [Obsolete("It's imprecise.", true)]
         * public async Task MousePressed(int offsetX, int offsetY, bool rightButton = false, uint pressedMillionSeconds = MousePressedTime, CancellationToken cancellationToken = default)
         * {
         *  MouseMove(offsetX, offsetY);
         *  await MousePressed(rightButton, pressedMillionSeconds, cancellationToken);
         * }
         */

        /// <summary>
        /// 鼠标滚轮
        /// </summary>
        /// <param name="wheelDelta">正值表明鼠标轮向前转动,即远离用户的方向;负值表明鼠标轮向后转动,即朝向用户。</param>
        public void MouseWheel(int wheelDelta = 500)
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            mouse_event(MouseEventFlag.Wheel, 0, 0, wheelDelta, UIntPtr.Zero);
            WindowsApi.WriteLog($"{nameof(MouseWheel)} {nameof(wheelDelta)}:{wheelDelta}");
        }
Пример #2
0
        /// <summary>
        /// 鼠标抬起
        /// </summary>
        /// <param name="rightButton">右键</param>
        public void MouseButtonUp(bool rightButton = false)
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            mouse_event(rightButton ? MouseEventFlag.RightUp : MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
            WindowsApi.WriteLog($"{nameof(MouseButtonUp)} {GetButtonString(rightButton)}");
        }
Пример #3
0
        /// <summary>
        /// Activate the form using the form handle.
        /// </summary>
        /// <param name="hWnd">form handle</param>
        public void SwitchToThisWindow(IntPtr hWnd)
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            //激活显示在最前面
            SwitchToThisWindow(hWnd, true);
            WindowsApi.WriteLog($"{nameof(SwitchToThisWindow)} handle is {hWnd}.");
        }
Пример #4
0
        /*
         * /// <summary>
         * /// 鼠标移动到相对当前鼠标的位置
         * /// </summary>
         * /// <param name="offsetX">offsetX</param>
         * /// <param name="offsetY">offsetY</param>
         * [Obsolete("It's imprecise.", true)]
         * public void MouseMove(int offsetX, int offsetY)
         * {
         *  if (WindowsApi.Delay.HasValue)
         *  {
         *      Thread.Sleep(WindowsApi.Delay.Value);
         *  }
         *
         *  mouse_event(MouseEventFlag.Move, offsetX, offsetY, 0, UIntPtr.Zero);
         *  WindowsApi.WriteLog($"{nameof(MouseMove)} {nameof(offsetX)}:{offsetX},{nameof(offsetY)}:{offsetY}");
         * }
         */

        /// <summary>
        /// 鼠标移动到绝对位置
        /// </summary>
        /// <param name="point">鼠标要移动到的绝对位置</param>
        /// <param name="delayPerPixel">每像素停留时间</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns></returns>
        public async Task MouseMove(MousePoint point, uint delayPerPixel, CancellationToken cancellationToken = default)
        {
            if (WindowsApi.Delay.HasValue)
            {
                await Task.Delay(WindowsApi.Delay.Value, cancellationToken);
            }

            await Task.Run(async() =>
            {
                var currentPoint = GetCurrentMousePoint();
                var currentX     = currentPoint.X;
                var currentY     = currentPoint.Y;

                var delayTimeSpan = TimeSpan.FromMilliseconds(delayPerPixel);

                while (currentPoint != point)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (currentX < point.X)
                    {
                        currentX++;
                    }
                    else if (currentX > point.X)
                    {
                        currentX--;
                    }

                    if (currentY < point.Y)
                    {
                        currentY++;
                    }
                    else if (currentY > point.Y)
                    {
                        currentY--;
                    }

                    currentPoint = new MousePoint(currentX, currentY);
                    var bounds   = _innerScreenApi.Value.GetMouseScreen(currentPoint).Bounds;
                    mouse_event(MouseEventFlag.Absolute | MouseEventFlag.Move, currentPoint.X * (MagicNumber / bounds.Width),
                                currentPoint.Y * (MagicNumber / bounds.Height),
                                0, UIntPtr.Zero);

                    if (delayPerPixel > 0u)
                    {
                        await Task.Delay(delayTimeSpan, cancellationToken);
                    }
                }

                WindowsApi.WriteLog(
                    $"{nameof(MouseMove)} from {nameof(MousePoint.X)}:{currentPoint.X},{nameof(MousePoint.Y)}:{currentPoint.Y} to {nameof(MousePoint.X)}:{point.X},{nameof(MousePoint.Y)}:{point.Y}");
            }, cancellationToken);
        }
Пример #5
0
        /// <summary>
        /// 鼠标移动到绝对位置
        /// </summary>
        /// <param name="point">需要移动到的坐标</param>
        public void MouseMove(MousePoint point)
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            var bounds = _innerScreenApi.Value.GetMouseScreen(point).Bounds;

            mouse_event(MouseEventFlag.Absolute | MouseEventFlag.Move, point.X * (MagicNumber / bounds.Width),
                        point.Y * (MagicNumber / bounds.Height),
                        0, UIntPtr.Zero);
            WindowsApi.WriteLog($"{nameof(MouseMove)} {nameof(MousePoint.X)}:{point.X},{nameof(MousePoint.Y)}:{point.Y}");
        }
Пример #6
0
        /*
         * /// <summary>
         * /// 鼠标双击
         * /// </summary>
         * /// <param name="offsetX">offsetX</param>
         * /// <param name="offsetY">offsetY</param>
         * /// <param name="rightButton">右键</param>
         * [Obsolete("It's imprecise.", true)]
         * public void MouseDoubleClick(int offsetX, int offsetY, bool rightButton = false)
         * {
         *  MouseMove(offsetX, offsetY);
         *  MouseDoubleClick(rightButton);
         * }
         */

        /// <summary>
        /// 鼠标按压
        /// </summary>
        /// <param name="rightButton">右键</param>
        /// <param name="pressedMillionSeconds">按压时长</param>
        /// <param name="cancellationToken">cancellationToken</param>
        public async Task MousePressed(bool rightButton = false, uint pressedMillionSeconds = MousePressedTime, CancellationToken cancellationToken = default)
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            await Task.Run(async() =>
            {
                mouse_event(rightButton ? MouseEventFlag.RightDown : MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
                await Task.Delay(TimeSpan.FromMilliseconds(pressedMillionSeconds), cancellationToken);
                mouse_event(rightButton ? MouseEventFlag.RightUp : MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);

                WindowsApi.WriteLog(
                    $"{nameof(MousePressed)} {GetButtonString(rightButton)} {nameof(MousePressedTime)}:{pressedMillionSeconds}");
            }, cancellationToken);
        }
Пример #7
0
        /*
         *
         * /// <summary>
         * /// Activate the process if it not minimize.
         * /// </summary>
         * /// <param name="process">process</param>
         * /// <returns>success</returns>
         * public bool SetForegroundWindow(Process process)
         * {
         *  var result = false;
         *
         *  if (WindowsApi.Delay.HasValue)
         *  {
         *      Thread.Sleep(WindowsApi.Delay.Value);
         *  }
         *
         *  if (process != null)
         *  {
         *      result = SetForegroundWindow(process.MainWindowHandle);
         *      WindowsApi.WriteLog(
         *          $"{nameof(SetForegroundWindow)} {nameof(process)} name is {process.ProcessName}, main window handle is {process.MainWindowHandle}, {nameof(result)} is {result}.");
         *  }
         *  else
         *  {
         *      WindowsApi.WriteLog($"{nameof(SetForegroundWindow)} {nameof(process)} is null.");
         *  }
         *
         *  return result;
         * }
         *
         * /// <summary>
         * /// Activate the form using the form handle if it not minimize.
         * /// </summary>
         * /// <param name="hWnd">form handle</param>
         * /// <returns>success</returns>
         * public bool SetForegroundWindowWithHandle(IntPtr hWnd)
         * {
         *  if (WindowsApi.Delay.HasValue)
         *  {
         *      Thread.Sleep(WindowsApi.Delay.Value);
         *  }
         *
         *  var result = SetForegroundWindow(hWnd);
         *  WindowsApi.WriteLog($"{nameof(SetForegroundWindow)} handle is {hWnd}, {nameof(result)} is {result}.");
         *
         *  return result;
         * }
         *
         */

        #endregion

        /// <summary>
        /// Activate the process.
        /// </summary>
        /// <param name="process"></param>
        public void SwitchToThisWindow(Process process)
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            if (process != null)
            {
                SwitchToThisWindow(process.MainWindowHandle, true);
                WindowsApi.WriteLog(
                    $"{nameof(SwitchToThisWindow)} {nameof(process)} name is {process.ProcessName}, main window handle is {process.MainWindowHandle}.");
            }
            else
            {
                WindowsApi.WriteLog($"{nameof(SwitchToThisWindow)} {nameof(process)} is null.");
            }
        }
Пример #8
0
        /// <summary>
        /// 获取当前的鼠标坐标
        /// </summary>
        /// <returns></returns>
        public MousePoint GetCurrentMousePoint()
        {
            if (WindowsApi.Delay.HasValue)
            {
                Thread.Sleep(WindowsApi.Delay.Value);
            }

            if (GetCursorPos(out var p))
            {
                WindowsApi.WriteLog($"{nameof(GetCurrentMousePoint)} {nameof(MousePoint.X)}:{p.X},{nameof(MousePoint.Y)}:{p.Y}");
                return(p);
            }
            else
            {
                WindowsApi.WriteLog($"{nameof(GetCurrentMousePoint)} operating failed.");
                return(new MousePoint());
            }
        }
Пример #9
0
        /// <summary>
        /// Screen capture to stream
        /// </summary>
        /// <param name="screen">The screen want to capture</param>
        /// <param name="imageFormat">Save image file path</param>
        /// <param name="bounds">bounds</param>
        /// <param name="timeOut">timeOut</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns></returns>
        public async Task <Stream> ScreenCaptureToStream(Screen screen, ImageFormat imageFormat = null,
                                                         Rectangle?bounds = null,
                                                         TimeSpan?timeOut = null,
                                                         CancellationToken cancellationToken = default)
        {
            if (WindowsApi.Delay.HasValue)
            {
                await Task.Delay(WindowsApi.Delay.Value, cancellationToken);
            }

            var linkedToken =
                timeOut == null
                    ? cancellationToken
                    : CancellationTokenSource
                .CreateLinkedTokenSource(cancellationToken, new CancellationTokenSource(timeOut.Value).Token)
                .Token;

            using (var screenPixel = await InnerScreenCapture(GetValidIntersectRectangle(screen, bounds), linkedToken))
            {
                if (screenPixel != null)
                {
                    linkedToken.ThrowIfCancellationRequested();
                    var stream = new MemoryStream();

                    if (imageFormat != null)
                    {
                        screenPixel.Save(stream, imageFormat);
                        WindowsApi.WriteLog(
                            $"{nameof(ScreenCaptureToStream)} save to stream with {nameof(imageFormat)}:{imageFormat}");
                    }
                    else
                    {
                        screenPixel.Save(stream, ImageFormat.Bmp);
                        WindowsApi.WriteLog(
                            $"{nameof(ScreenCaptureToStream)} save to stream with {nameof(imageFormat)}:{ImageFormat.MemoryBmp}");
                    }

                    return(stream);
                }
            }

            return(null);
        }