c# 模拟鼠标操作

       /*定义参数*/
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        /*调用AIP函数*/

        /*鼠标点击的函数*/
        [DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        /*设置鼠标的坐标*/
        [DllImport("user32.dll")]
        public static extern void SetCursorPos(int dx, int dy);

        /*获取鼠标的坐标*/
        [DllImport("user32.dll")]
        public static extern void GetCursorPos(ref Point lpPoint);

        private void button1_Click(object sender, EventArgs e)
        {
                        SetCursorPos(120,120);//设置鼠标的坐标

                        //模拟左击
                        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

                        Point point = new Point();
                        GetCursorPos(ref point);//获得鼠标位置
        }

你可能感兴趣的:(C#,模拟鼠标)