C#Winform调用Windows API user32.dll实现鼠标事件示例

1、背景

之前工作时需要固定点击屏幕上一位置的button,即使人不在工位也需要,就想起了按键精灵,但转念一想自己会使用C#为何不自行写一个小工具呢,那么分享給大家!

2、代码

小工具没有什么难点,主要是调用Windows API user32.dll动态库,将其引入到项目之中,此为非托管代码,使用DllImport特性即可。这里也简单介绍下user32动态库,user32.dll是Windows用户界面相关应用程序接口,用于包括Windows处理,基本用户界面等特性,如创建窗口和发送消息,或者是将鼠标的点击信号传递给相应的窗口。可以查看哥们的文章:user32.dll 函数说明,指示封装在dll内的各个函数用处。然后代码就是对时间的处理,都知道Thread.Sleep()参数是毫秒,处理一下为秒,因为咱不想多输入3个0,那么就是秒转毫秒,乘以1000,即在textBox里面输入一个数字就行啦。
剩下就是逻辑处理~

#region 非托管引用
        [DllImport("user32.dll")]
        //合成鼠标移动和按扭菜单事件
        private static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
        [DllImport("user32.dll")]
        //将光标移到指定的屏幕坐标处
        private static extern bool SetCursorPos(int X, int Y);

        [Flags]
        private 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
        }

        #endregion

        //输入的数字,即秒数
        private int time_set;
        //秒数*1000=毫秒,为满足Sleep()
        private int time_run;
        private Thread th_0;
        private bool rep_ = true;
        private bool flag_ = true;

        public Form1()
        {
            InitializeComponent();
            this.Text = "帮你点(*^▽^*)";
            btn_ShutDown.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //用于显示坐标
            label2.Text = MousePosition.X.ToString().Trim();
            label3.Text = MousePosition.Y.ToString().Trim();
        }

        private void btn_AutoClick_Click(object sender, EventArgs e)
        {
            if (flag_)
            {
                int.TryParse(textBox1.Text, out time_set);
                //bool a = int.TryParse(textBox1.Text, out time_set);
                time_run = time_set * 1000;
                if (textBox1.Text == null || time_run <= 2000)
                {
                    MessageBox.Show("不能为空,或小于等于2秒", "注意!", MessageBoxButtons.OK);
                    return;
                }
                else if (time_run >= 10000)
                {
                    MessageBox.Show("设置点击间隔时间过长!", "注意!", MessageBoxButtons.OK);
                }
                else if (textBox3.Text == null || textBox4.Text == null)
                {
                    MessageBox.Show("请输入坐标!", "注意!", MessageBoxButtons.OK);
                    return;
                }

                th_0 = new Thread(new ThreadStart(Mouse_Event));
                th_0.Start();
                th_0.Name = "AutoClick_Start";
                th_0.IsBackground = true;

                btn_AutoClick.Text = "运 行 中...";
                btn_AutoClick.BackColor = Color.GreenYellow;
                //btn_ShutDown.Enabled = true;
                //btn_ShutDown.BackColor = Color.Orange;
                textBox2.Text = "输入的时长是: " + time_set + "秒";
                flag_ = false;
            }
            else
            {
                th_0.Abort();
                btn_AutoClick.Text = "已 停 止!!";
                btn_AutoClick.BackColor = Color.Red;
                flag_ = true;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox3.Text = MousePosition.X.ToString().Trim();
                textBox4.Text = MousePosition.Y.ToString().Trim();
            }
        }

        private void btn_01_Click(object sender, EventArgs e)
        {
            if (rep_)
            {
                //修改固定像素坐标
                textBox3.Text = "1110";
                textBox4.Text = "820";
                rep_ = false;
            }
            else
            {
                textBox3.Text = null;
                textBox4.Text = null;
                rep_ = true;
            }
        }

        private void btn_ShutDown_Click(object sender, EventArgs e)
        {
            th_0.Abort();
        }

        public void Mouse_Event()
        {
            int.TryParse(textBox1.Text, out time_run);
            time_run = time_set * 1000;
            while (true)
            {
                Thread.Sleep(time_run);
                int.TryParse(textBox3.Text, out int X);
                int.TryParse(textBox4.Text, out int Y);
                SetCursorPos(X, Y);
                //自动按下的按键
                mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
                mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
                mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.Absolute, 0, 0, 0, UIntPtr.Zero);
                mouse_event(MouseEventFlag.LeftUp | MouseEventFlag.Absolute, 0, 0, 0, UIntPtr.Zero);
            }
        }

**

3、效果展示

在这里插入图片描述
在这里插入图片描述
红框内设置需要点击的坐标像素点,建议时间间隔稍微长一点,以便能手动控制鼠标哈哈()
程序拿去自定义修改吧~
源码下载~

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值