110,533
社区成员
发帖
与我相关
我的任务
分享
/// <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;
}
}
}
}