C#控制鼠标自动连续点(DEMO)

---------------------------界面---------------------------------------------------- 

 

 

------------------------------------------------------后台代码-------------------------------------

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MouseDo
{

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

//结构体布局 本机位置

[StructLayout(LayoutKind.Sequential)]

struct NativeRECT

{

public int left;

public int top;

public int right;

public int bottom;

}

 

//将枚举作为位域处理

[Flags]

enum MouseEventFlag : uint //设置鼠标动作的键值

{

Move = 0x0001, //发生移动

LeftDown = 0x0002, //鼠标按下左键

LeftUp = 0x0004, //鼠标松开左键

RightDown = 0x0008, //鼠标按下右键

RightUp = 0x0010, //鼠标松开右键

MiddleDown = 0x0020, //鼠标按下中键

MiddleUp = 0x0040, //鼠标松开中键

XDown = 0x0080,

XUp = 0x0100,

Wheel = 0x0800, //鼠标轮被移动

VirtualDesk = 0x4000, //虚拟桌面

Absolute = 0x8000

}

 

//设置鼠标位置

[DllImport("user32.dll")]

static extern bool SetCursorPos(int X, int Y);

 

//设置鼠标按键和动作

[DllImport("user32.dll")]

static extern void mouse_event(MouseEventFlag flags, int dx, int dy,

uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型

 

[DllImport("user32.dll")]

static extern IntPtr FindWindow(string strClass, string strWindow);

 

//该函数获取一个窗口句柄,该窗口雷鸣和窗口名与给定字符串匹配 hwnParent=Null从桌面窗口查找

[DllImport("user32.dll")]

static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,

string strClass, string strWindow);

 

[DllImport("user32.dll")]

static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);

 

//定义变量

const int AnimationCount = 80;

private Point endPosition;

private int count;

int flg = 0;

//开始按钮
private void button1_Click(object sender, EventArgs e)
{

if (string.IsNullOrEmpty(textBox1.Text)&& string.IsNullOrEmpty(textBox2.Text))//没有坐标
{
MessageBox.Show("请先按F7,F8获取坐标!");
}
if (!string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))//只有坐标1
{
if (dowhile.Checked == true)
{
flg = 0;
do
{
moudown(textBox1);
if (!string.IsNullOrEmpty(textBox3.Text))
{
int time = Convert.ToInt32(textBox3.Text) * 1000;
Thread.Sleep(time);
}
} while (flg==0);
}
else
{
moudown(textBox1);
}

}
else if (string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))//只有坐标1
{
if (dowhile.Checked == true)
{
flg = 0;
do
{
moudown(textBox2);
if (!string.IsNullOrEmpty(textBox3.Text))
{
int time = Convert.ToInt32(textBox3.Text) * 1000;
Thread.Sleep(time);
}
} while (flg == 0);
}
else
{
moudown(textBox2);
}
}
else//坐标1,2
{
if (dowhile.Checked == true)
{
flg = 0;
do
{
moudown(textBox1,textBox2);
if (!string.IsNullOrEmpty(textBox3.Text))
{
int time = Convert.ToInt32(textBox3.Text) * 1000;
Thread.Sleep(time);
}
} while (flg == 0);
}
else
{
moudown(textBox1,textBox2);
}
}
}

//键盘获取屏幕鼠标坐标
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
Point screenPoint = Control.MousePosition;// /

textBox1.Text = screenPoint.X.ToString()+","+screenPoint.Y.ToString();
}
if (e.KeyCode == Keys.F8)
{
Point screenPoint = Control.MousePosition;//
textBox2.Text = screenPoint.X.ToString() + "," + screenPoint.Y.ToString();
}
if (e.KeyCode == Keys.Escape)
{
flg = 1;
}
}
//单坐标执行
void moudown(TextBox TB)
{
string[] a = TB.Text.Split(',');
int x = Convert.ToInt32(a[0]);
int y = Convert.ToInt32(a[1]);
SetCursorPos(x, y);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}

//双坐标执行
void moudown(TextBox TB1, TextBox TB2)
{
string[] a = TB1.Text.Split(',');
int x = Convert.ToInt32(a[0]);
int y = Convert.ToInt32(a[1]);

string[] a1 = TB2.Text.Split(',');
int x1 = Convert.ToInt32(a1[0]);
int y1 = Convert.ToInt32(a1[1]);

SetCursorPos(x, y);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(500);
SetCursorPos(x1, y1);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);

}

//控制停止循环
private void button2_Click(object sender, EventArgs e)
{
flg = 1;
}


}
}

posted @ 2019-06-21 16:20  后跳  阅读(1791)  评论(2编辑  收藏  举报