• C#__ 模拟鼠标单击事件


    首先要用到的引用有

    [DllImport("User32")]
    public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
    [DllImport("User32")]
    public extern static void SetCursorPos(int x, int y);

    1、SetCursorPos:用于设置鼠标的坐标。与它对应的当然就是GetCursorPos啦!

    2、mouse_event:这个事件是用于模仿鼠标的各种操作(单击、双击....),视输入参数的不同而定了。

     =============================================================================================

    下面是完整的代码:

    [DllImport("User32")]
    public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
    [DllImport("User32")]
    public extern static void SetCursorPos(int x, int y);
    [DllImport("User32")]
    public extern static bool GetCursorPos(out POINT p);
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
    public int X;
    public int Y;
    }
    public enum MouseEventFlags
    {
    Move = 0x0001, //移动鼠标
    LeftDown = 0x0002,//模拟鼠标左键按下
    LeftUp = 0x0004,//模拟鼠标左键抬起
    RightDown = 0x0008,//鼠标右键按下
    RightUp = 0x0010,//鼠标右键抬起
    MiddleDown = 0x0020,//鼠标中键按下 
    MiddleUp = 0x0040,//中键抬起
    Wheel = 0x0800,
    Absolute = 0x8000//标示是否采用绝对坐标
    }
    private void button1_Click(object sender, EventArgs e)
    {
    // POINT p = new POINT();
    // GetCursorPos(out p);
    // MessageBox.Show(p.X.ToString()+":"+p.Y.ToString());
    AutoClick();
    }
    
    public static void AutoClick()
    {
    while (true)
    {
    
    //设置鼠标的坐标
    
    SetCursorPos(72, 40);
    
    //这里模拟的是一个鼠标双击事件
    mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
    mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
    
    mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
    mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
    }
    }
  • 相关阅读:
    matplotlib.pyplot---------Python强大的绘图功能软件
    Python常用的几种数据结构-链表,数组,字典
    Python实现矩阵
    Linux常用命令----存
    观《解忧杂货店》有感
    js格式化日期
    读取web应用中properties配置文件(这种方法可能不是最好的)
    easyui 获得ComboBox选中项的值 getValue
    request 报错The remote server returned an error: (415) Unsupported Media Type.
    No mapping found for HTTP request with URI [/spring/WEB-INF/page/index.jsp] in DispatcherServlet with name 'spring'
  • 原文地址:https://www.cnblogs.com/cindyLu/p/3180410.html
Copyright © 2020-2023  润新知