c# 调用API mouse

    技术2025-06-02  89

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Runtime.InteropServices;

    namespace WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

            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; //标示是否采用绝对坐标

     

            public Form1()

            {

                InitializeComponent();

            }

     

              

            private void button1_Click(object sender, EventArgs e)

            {

                mouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_ABSOLUTE, 500, 400, 0, 0);

                mouse_event(MOUSEEVENTF_LEFTUP , 500, 400, 0, 0);

            }

     

       [DllImport("user32", EntryPoint = "mouse_event")]

        private static extern int mouse_event(

            int dwFlags,// 下表中标志之一或它们的组合

            int dx,

            int dy, //指定xy方向的绝对位置或相对位置

            int cButtons,//没有使用

            int dwExtraInfo//没有使用

        );

       }

    }

    最新回复(0)