学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

用C#模拟的鼠标点击

本主题由 静夜思 于 2008-4-21 19:14 拒绝承认原创

用C#模拟的鼠标点击

复制内容到剪贴板
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 鼠标单击模拟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }
        [DllImport("user32.dll")]
        public static extern void mouse_event
            (
            int dwFlags,
            int dx,
            int dy,
            int dwData,
            int dwExtraInfo
            );
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);  

        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;
       private void button1_Click(object sender, EventArgs e)
        {
             timer2.Start();
            if (textBox3.Text != "")
                timer3.Start();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            int x,y;
            string[] str;
            str=textBox2.Text.Split(',');
            x=Convert.ToInt32(str[0]);
            y=Convert.ToInt32(str[1]);
            SetCursorPos(x, y);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Point pos = Cursor.Position;
            textBox1.Text = pos.X.ToString() +','+ pos.Y.ToString();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            int x=0, y=0;
            string[] sta;
            sta = textBox3.Text.Split(',');
            x = Convert.ToInt32(sta[0]);
            y = Convert.ToInt32(sta[1]);
            SetCursorPos(x, y);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer2.Stop();
            timer3.Stop();
        }
    }
}
输入2个坐标可以在屏幕的那2个坐标位置重复点击。
附件: 您所在的用户组无法下载或查看附件

TOP

收下~ 顶一个

TOP

顶啊 学习啊

TOP

taiku

TOP

发新话题