今天在网上看到有人问C#如何实现鼠标左键自动点击并自己设定点击间隔,我也感到很好奇,查了一下发现很简单,只需利用WIN32 API的两个函数SetCursorPos和mouse_event操作鼠标:

调用方法如下:

using System.Runtime.InteropServices;

[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);  
        [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  
        }

 

其中SetCursorPos使鼠标移动到指定位置;mouse_event使用MouseEventFlag枚举中的Move,也可以使鼠标移动。

mouse_event中使用不同的枚举值可以模拟不同的鼠标事件。

值得注意的是有几点:

1. 我们不能用mouse_event(MouseEventFlag.LeftDown, 10, 10, 0, UIntPtr.Zero);去模拟在(10, 10)处的左键事件,我们需要把这步拆成两步:

第一步:移动鼠标到(10,10)处,用SetCursorPos(10, 10);

第二步:触发左键,用mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);

本质上是两步的事件,不能把window API 想的太聪明,认为它会自动跑到(10,10)处,再左键

2. MouseEventFlag的枚举值可以多个一起用,使用 | 操作符

鼠标左键按下和松开两个事件的组合即一次单击:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
两次连续的鼠标左键单击事件 构成一次鼠标双击事件:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )

 


3. MouseEventFlag中有个Absolute枚举,如果没指定Absolute, 则mouse_event的操作是相对于上次鼠标所在的位置;如果指定了Absolute,则是相对于整个屏幕坐标的位置。

这里要注意,指定Absolute,鼠标的坐标会被约束在[0, 65535]之间。0即对应屏幕左,65535即对应屏幕右下角。

所以模拟在(10, 10)处的左键,代码应改为:

mouse_event(MOUSEEVENTF_LEFTDOWN, 10 * 65536 / Screen.PrimaryScreen.Bounds.Width, 10 * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);

 

知道了这些之后,事情就变得相当简单了,可以建一个线程来控制鼠标的事件。完整代码如下:

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

namespace ControlMouse
{
    public partial class Form1 : Form
    {
        [DllImport("User32")]
        public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);

        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);

        [DllImport("User32")]
        public extern static bool GetCursorPos(out POINT p);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
        }
       
        public enum MouseEventFlags
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            Wheel = 0x0800,
            Absolute = 0x8000
        }

        //在(x,y)处单击
        private void AutoClick(int x, int y)
        {
            POINT p = new POINT();
            GetCursorPos(out p);
            try
            {
                SetCursorPos(x, y);
                mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
                mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
            }
            finally
            {
                SetCursorPos(p.X, p.Y);
            }
        }

        Point CursorPosition = new Point(0, 0);       

        Thread controlThread; //控制鼠标的线程

        private void ThreadRunMethod()
        {
            while (true)
            {
                CursorPosition = Cursor.Position;
                Thread.Sleep(5000);   //时间间隔
                AutoClick(CursorPosition.X, CursorPosition.Y);              
                Thread.Sleep(5000);
                AutoClick(CursorPosition.X + 2, CursorPosition.Y - 15);                               
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                controlThread = new Thread(new ThreadStart(ThreadRunMethod));
                controlThread.Start();
            }
            catch (Exception)
            {
                Application.DoEvents();
            }
        }      
    }
}

这样就可以让鼠标在两个位置反复的点击。至于怎么利用这些刷人气,想必我就不用多说了吧。提醒一下,把时间间隔设的长一点,不然会被封的。刚刚随便测试了一小时,人气多了1200 ^-^

 

posted on 2010-12-01 14:08  freedom831215  阅读(415)  评论(0编辑  收藏  举报