C#实现鼠标的自动循环指定操作

来源:互联网 发布:php删除数据库指定元素 编辑:程序博客网 时间:2024/06/05 08:57
C#实现鼠标的自动循环指定操作

本文将以写过的一款实用软件:鼠标自动指定操作的实现为例,详细介绍C#如何实现对鼠标的操作,同时包括对键盘事件的捕捉。
废话不多说,先给大家看看实现的软件功能:


如上图所示:该款软件可设定鼠标单击还是双击、自动点击速度、需要循环点击的位置(通过捕捉键盘的1234键确认鼠标位置)。并且可依据键盘事件中e的监听停止鼠标的循环点击。

首先需要定义一些从非托管DLL导出来的函数:

        //是鼠标自动移动到某个位置        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]        public extern static bool SetCursorPos(int x, int y);        //获取鼠标当前的位置        [StructLayout(LayoutKind.Sequential)]        public struct POINT        {            public int X;            public int Y;        }        [DllImport("User32")]        public extern static bool GetCursorPos(out POINT p);        //是否显示鼠标箭头        [DllImport("User32")]        public extern static int ShowCursor(bool bShow);        //调用系统函数 模拟鼠标事件函数        [DllImport("user32", EntryPoint = "mouse_event")]        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);


详细实现可参见我的上一篇博客:http://blog.csdn.net/laozhuxinlu/article/details/50429743。

接下来就是获取指定需要单击或者双击的位置。
首先定义四个私有int型参数:
 private int x1,y1,x2,y2,x3,y3,x4,y4;

然后添加键盘监听事件KeyPress:
   private void Automatic_mouse_click_KeyPress(object sender, KeyPressEventArgs e)        {            if(e.KeyChar == '1')            {                //获取当前鼠标的位置                POINT p = new POINT();                GetCursorPos(out p);                x1 = p.X;                y1 = p.Y;                textBox1.Text = "" + p.X + "--" + p.Y;            }            if (e.KeyChar == '2')            {                //获取当前鼠标的位置                POINT p = new POINT();                GetCursorPos(out p);                x2 = p.X;                y2 = p.Y;                textBox2.Text = "" + p.X + "--" + p.Y;            }            if (e.KeyChar == '3')            {                //获取当前鼠标的位置                POINT p = new POINT();                GetCursorPos(out p);                x3 = p.X;                y3 = p.Y;                textBox3.Text = "" + p.X + "--" + p.Y;            }            if (e.KeyChar == '4')            {                //获取当前鼠标的位置                POINT p = new POINT();                GetCursorPos(out p);                x4 = p.X;                y4 = p.Y;                textBox4.Text = "" + p.X + "--" + p.Y;            }            if (e.KeyChar == 's')            {                times = 0;                timer1.Enabled = true;                             }            if (e.KeyChar == 'e')            {                timer1.Enabled = false;             }        }

数字键1/2/3/4实现对鼠标位置的捕获,s键开启定时器开始循环点击,e键取消定时器的循环。

单击、双击的设定:
   private void radioButton1_CheckedChanged(object sender, EventArgs e)        {            one_or_two = true;        }        private void radioButton2_CheckedChanged(object sender, EventArgs e)        {            one_or_two = false;        }

鼠标点击快慢的设定(主要是通过checkBox控件实现对定时器timer1的属性Interval进行赋值更改选择):
 private void checkBox1_CheckedChanged(object sender, EventArgs e)        {                     checkBox2.Checked = false;            checkBox3.Checked = false;                   timer1.Interval = 2000;        }        private void checkBox2_CheckedChanged(object sender, EventArgs e)        {            checkBox1.Checked = false;            checkBox3.Checked = false;            timer1.Interval = 1000;        }        private void checkBox3_CheckedChanged(object sender, EventArgs e)        {            checkBox1.Checked = false;            checkBox2.Checked = false;            timer1.Interval = 600;        }

源代码资源:http://download.csdn.net/detail/laozhuxinlu/9510294。



0 0