C#鼠标控制

该代码示例展示了如何在C#中使用WinAPI函数mouse_event、SetCursorPos和GetCursorPos来模拟鼠标移动、左键点击、左键双击以及滚轮滚动操作。通过这些函数,可以实现对鼠标的程序化控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WQ
{
    /// <summary>
    /// 鼠标
    /// </summary>
    public class Mouse
    {
        // https://blog.youkuaiyun.com/fuhanghang/article/details/118700282
        // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event

        private const int MOUSEEVENTF_MOVE       = 0x0001;      // 移动鼠标
        private const int MOUSEEVENTF_LEFTDOWN   = 0x0002;      // 鼠标左键按下
        private const int MOUSEEVENTF_LEFTUP     = 0x0004;      // 鼠标左键抬起
        private const int MOUSEEVENTF_RIGHTDOWN  = 0x0008;      // 鼠标右键按下
        private const int MOUSEEVENTF_RIGHTUP    = 0x0010;      // 鼠标右键抬起
        private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;      // 鼠标中键按下
        private const int MOUSEEVENTF_MIDDLEUP   = 0x0040;      // 鼠标中键抬起
        private const int MOUSEEVENTF_WHEEL      = 0x0800;      // 滑动鼠标滚轮
        private const int MOUSEEVENTF_ABSOLUTE   = 0x8000;      // 绝对坐标

        [DllImport("user32.dll")]
        private extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);



        //  https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setcursorpos?redirectedfrom=MSDN

        /// <summary>
        /// 将光标移动到指定的屏幕坐标。
        /// </summary>
        /// <param name="X">屏幕坐标</param>
        /// <param name="Y">屏幕坐标</param>
        /// <returns>如果成功,则返回非零;否则返回零。</returns>
        [DllImport("user32.dll")]
        public extern static int SetCursorPos(int X, int Y);



        //  https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-getcursorpos?redirectedfrom=MSDN

        /// <summary>
        /// 定义一个屏幕坐标。
        /// </summary>
        public struct POINT
        {
            public int X;
            public int Y;
        }

        /// <summary>
        /// 获取光标的屏幕坐标。
        /// </summary>
        /// <param name="lpPoint">屏幕坐标</param>
        /// <returns>如果成功,则返回非零;否则返回零。</returns>
        [DllImport("user32.dll")]
        public extern static int GetCursorPos(ref POINT lpPoint);





        /// <summary>
        /// 鼠标左键单击
        /// </summary>
        /// <param name="x">屏幕坐标</param>
        /// <param name="y">屏幕坐标</param>
        public static void Button_Left_Click(int x, int y)
        {
            SetCursorPos(x, y);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }

        /// <summary>
        /// 鼠标左键双击
        /// </summary>
        /// <param name="x">屏幕坐标</param>
        /// <param name="y">屏幕坐标</param>
        public static void Button_Left_DoubleClick(int x, int y)
        {
            SetCursorPos(x, y);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }

        /// <summary>
        /// 滑动滚轮
        /// </summary>
        /// <param name="x">屏幕坐标</param>
        /// <param name="y">屏幕坐标</param>
        /// <param name="number">滚轮移动量。正值表示滚轮向前旋转,远离用户;负值表示轮子向后旋转,朝向用户。一次滚轮点击被定义为WHEEL_DELTA,值为120。</param>
        public static void Wheel(int x, int y, int number)
        {
            SetCursorPos(x, y);
            mouse_event(MOUSEEVENTF_WHEEL, 0, 0, number, 0);
        }

    }

}
WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值