模拟鼠标操作

Posted on 2019-01-18 20:29  努力成长静待花开  阅读(320)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:
  API函数mouse_event    //模拟鼠标的事件

[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public extern static IntPtr mouse_event(Int32 dwFlag,Int32 dx,Int32 dy,Int32 dwData,Int32 dwExtraInfo);

实现代码:

        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
        public extern static IntPtr mouse_event(Int32 dwFlag,Int32 dx,Int32 dy,Int32 dwData,Int32 dwExtraInfo);
        public const int MOOSEEVENTF_MOVE = 0x0001;             //表示鼠标移动
        public const int MOOSEEVENTF_LEFTDOWN = 0x0002;         //表示鼠标左键按下
        public const int MOOSEEVENTF_LEFTUP = 0x0004;           //表示鼠标左键松开
        private void button1_Click(object sender, EventArgs e)
        {
            mouse_event(MOOSEEVENTF_MOVE,-20,-20,0,0);
            mouse_event(MOOSEEVENTF_LEFTDOWN,0,0,0,0);
            mouse_event(MOOSEEVENTF_LEFTUP,0,0,0,0);
            mouse_event(MOOSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOOSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }