C# 有什么办法可以实现模拟鼠标

小瓜12 2015-01-09 02:25:56
C# 有什么办法可以实现模拟鼠标
是模拟鼠标移动 不是一闪忽然出现的 有轨道的那种 慢慢移动过去的 可以类似按键精灵的那种

有什么办法可以实现吗?有这方面的资料吗 应该从哪里入手

...全文
253 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
willhuo 2015-01-10
  • 打赏
  • 举报
回复

 /// <summary>
        /// 人工模拟移动鼠标,有鼠标轨迹和延时
        /// </summary>
        /// <param name="currentCursor">当前鼠标光标坐标</param>
        /// <param name="Destination">目的鼠标光标坐标</param>
        public static void MoveMouseToPoint(Point currentCursor, Point Destination)
        {
            //如果鼠标移动轨迹需要添加随机性,需要增加相应的移动轨迹算法
            int xc = currentCursor.X - Destination.X;
            int yc = currentCursor.Y - Destination.Y;
            int offsetx, offsety;

            if (xc == 0 && yc == 0)
            {
                //bw.ReportProgress(0, string.Format("鼠标当前坐标就是目的坐标。"));
            }
            if (xc == 0 && yc != 0)
            {
                //bw.ReportProgress(0, string.Format("X坐标不移动,Y坐标移动。"));
                if (yc > 0)
                {
                    offsetx = 0;
                    offsety = -2;
                    while (true)
                    {
                        currentCursor.Y += offsety;
                        SetCursorPos(currentCursor.X, currentCursor.Y);
                        Thread.Sleep(5);

                        if (currentCursor.Y <= Destination.Y)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    offsetx = 0;
                    offsety = 2;
                    while (true)
                    {
                        currentCursor.Y += offsety;
                        SetCursorPos(currentCursor.X, currentCursor.Y);
                        Thread.Sleep(5);

                        if (currentCursor.Y >= Destination.Y)
                        {
                            break;
                        }
                    }
                }
            }
            if (xc != 0 && yc == 0)
            {
                //bw.ReportProgress(0, string.Format("X坐标移动,Y坐标不移动。"));
                if (xc > 0)
                {
                    offsetx = -2;
                    offsety = 0;
                    while (true)
                    {
                        currentCursor.X += offsetx;
                        SetCursorPos(currentCursor.X, currentCursor.Y);
                        Thread.Sleep(5);

                        if (currentCursor.X <= Destination.X)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    offsetx = 2;
                    offsety = 0;
                    while (true)
                    {
                        currentCursor.X += offsetx;
                        SetCursorPos(currentCursor.X, currentCursor.Y);
                        Thread.Sleep(5);

                        if (currentCursor.X >= Destination.X)
                        {
                            break;
                        }
                    }
                }
            }
            if (xc != 0 && yc != 0)
            {
                //bw.ReportProgress(0, string.Format("X坐标移动,Y坐标移动。"));
                if (xc > 0)
                {
                    offsetx = -2;
                }
                else
                {
                    offsetx = 2;
                }
                if (yc > 0)
                {
                    offsety = -2;
                }
                else
                {
                    offsety = 2;
                }

                while (true)
                {
                    currentCursor.X += offsetx;
                    currentCursor.Y += offsety;

                    //bw.ReportProgress(0,string.Format("X:{0},Y:{1}",currentCursor.X,currentCursor.Y));

                    SetCursorPos(currentCursor.X, currentCursor.Y);
                    Thread.Sleep(5);

                    //if (currentCursor.X == Destination.X)
                    if (Math.Abs(currentCursor.X - Destination.X) <= 2)
                    {
                        offsetx = 0;
                    }
                    if (Math.Abs(currentCursor.Y - Destination.Y) <= 2)
                    {
                        offsety = 0;
                    }
                    if (Math.Abs(currentCursor.X - Destination.X) <= 2 && Math.Abs(currentCursor.Y - Destination.Y) <= 2)
                    {
                        break;
                    }
                }
            }
        }
tcmakebest 2015-01-09
  • 打赏
  • 举报
回复
慢慢移就要计算每次移的坐标,用定时器操作
於黾 2015-01-09
  • 打赏
  • 举报
回复
比如timer设置100(每秒执行10次),先定义个变量设置好一次挪动多少,然后通过鼠标当前坐标和鼠标目标坐标计算要往哪个方向移动(下一个坐标),timer里设置鼠标挪到这个坐标上,这个过程就结束了,100ms之后再次执行 当然你需要一个bool变量控制一下,如果已经挪到地方了,就不再执行timer,可以在timer里用if判断,或者到地方就把timer先停掉,下次需要自动挪的时候重新开启
bdmh 2015-01-09
  • 打赏
  • 举报
回复
api函数SetCursorPos设置鼠标位置
於黾 2015-01-09
  • 打赏
  • 举报
回复
按键精灵的移动鼠标也是直接指定鼠标坐标,直接飞过去的,而不是一点一点挪过去 你想实现"挪"的效果,自己加timer做动画控制

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧