C#对鼠标的操作

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //获取双击间隔时间
        [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
        public extern static int GetDoubleClickTime(); 
        //获取鼠标键数目
        public const int SM_CMOUSEBUTTONS = 43;//定义一个常量值
        [DllImport("user32",EntryPoint="GetSystemMetrics")]
        public extern static int GetSystemMetrics(int intcoutn);
        private void Form1_Load(object sender, EventArgs e)
        {
            //设置定时器
            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.label1.Text ="鼠标双击间隔时间是:"+GetDoubleClickTime() + "ms";
            this.label2.Text = "鼠标键数是:" + GetSystemMetrics(SM_CMOUSEBUTTONS) + "";
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            //this.label3.Text = e.X.ToString();
            //this.label4.Text = e.Y.ToString();
        }
        //定时获取光标位置
        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(out Point lpPoint);
        //定时设置光标位置  
        [DllImport("user32")]
        static extern bool SetCursorPos(int X, int Y);
        //发送鼠标消息
        [DllImport("user32.dll")]
        //引入API函数mouse_event
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        const int MOUSEEVENTF_MOVE = 0x0001;//表示鼠标移动
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;//表示鼠标左键按下
        const int MOUSEEVENTF_LEFTUP = 0x0004;//表示鼠标左键松开

        private void timer1_Tick(object sender, EventArgs e)
        {
            Point ShowPoint = new Point();
            GetCursorPos(out ShowPoint);
            this.label3.Text = ShowPoint.X.ToString();
            this.label4.Text = ShowPoint.Y.ToString();
            //设置鼠标位置
            SetCursorPos(509,53);
            //发送左键按下消息
            mouse_event(MOUSEEVENTF_LEFTDOWN, 509, 53, 0, 0);
            //发送字符
            SendKeys.Send("a");
        }
    }
}

 

posted @ 2014-06-29 00:20  qq921201008  阅读(402)  评论(0编辑  收藏  举报