zoukankan      html  css  js  c++  java
  • C# 鼠标操作类

    C#鼠标操作类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApp1
    {
        /// <summary>
        /// C#鼠标操作类
        /// </summary>
        public class MouseOperations
        {
            [Flags]
            public enum MouseEventFlags
            {
                LeftDown = 0x00000002,
                LeftUp = 0x00000004,
                MiddleDown = 0x00000020,
                MiddleUp = 0x00000040,
                Move = 0x00000001,
                Absolute = 0x00008000,
                RightDown = 0x00000008,
                RightUp = 0x00000010
            }
    
            [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetCursorPos(int x, int y);
    
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool GetCursorPos(out MousePoint lpMousePoint);
    
            [DllImport("user32.dll")]
            private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
    
            /// <summary>
            /// 设置当前坐标
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            public static void SetCursorPosition(int x, int y)
            {
                SetCursorPos(x, y);
            }
    
            /// <summary>
            /// 设置当前坐标
            /// </summary>
            /// <param name="point"></param>
            public static void SetCursorPosition(MousePoint point)
            {
                SetCursorPos(point.X, point.Y);
            }
    
            /// <summary>
            /// 获取当前坐标
            /// </summary>
            /// <returns></returns>
            public static MousePoint GetCursorPosition()
            {
                MousePoint currentMousePoint;
                var gotPoint = GetCursorPos(out currentMousePoint);
                if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
                return currentMousePoint;
            }
    
            /// <summary>
            /// 鼠标事件
            /// </summary>
            /// <param name="value"></param>
            public static void MouseEvent(MouseEventFlags value)
            {
                MousePoint position = GetCursorPosition();
    
                mouse_event
                    ((int)value,
                     position.X,
                     position.Y,
                     0,
                     0)
                    ;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct MousePoint
            {
                public int X;
                public int Y;
    
                public MousePoint(int x, int y)
                {
                    X = x;
                    Y = y;
                }
            }
        }
    }

    用法:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using static ConsoleApp1.MouseOperations;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                MouseOperations.SetCursorPosition(200, 200);
                Thread.Sleep(2000);
                MouseOperations.SetCursorPosition(400, 400);
                Thread.Sleep(2000);
                MouseOperations.MouseEvent(MouseEventFlags.LeftDown);
            }
        }
    
    
    }
  • 相关阅读:
    [导入]google的网络U盘
    [导入]下载 Visual Studio 2005 Starter Kits,提高效率!
    TXT文件 是 好多列 组成,其中有一列是 身份证,如何 用EXCEL 打开时,不会变成 科学计数的形式?
    个人所得税2011新计算公式Excel版,及由税款倒推收入额
    ServU虚拟路径映射问题
    Access .mdb数据库 转成 SQLITE数据库
    undefined与null的区别
    从内存的角度来区分基本类型和引用类型的区别
    JAVA类加载和反射介绍
    onConfigurationChanged的作用
  • 原文地址:https://www.cnblogs.com/xytmj/p/14273445.html
Copyright © 2011-2022 走看看