using System.Runtime.InteropServices;

namespace Common.system
{
    public class MouseEventCommon
    {
        //[DllImport("user32.dll")]
        //static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        [DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        /// <summary>
        /// 设置鼠标的坐标 
        /// </summary>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern int SetCursorPos(int x, int y);

        /// <summary>   
        /// 获取鼠标的坐标   
        /// </summary>   
        /// <param name="lpPoint">传址参数，坐标point类型</param>   
        /// <returns>获取成功返回真</returns>   


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);

        /// <summary>
        /// 鼠标点击左键
        /// </summary>
        /// <param name="dx">坐标x</param>
        /// <param name="dy">坐标Y</param>
        /// <param name="data"></param>
        public static void MouseLefClickEvent(int dx, int dy, int data)
        {
            GetCursorPos(out POINT pointRecord);
            mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0);
            mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0);
        }
        public static void MouseText(int dx, int dy)
        {
            mouse_event((int)MouseEventFlag.LeftDown, dx, dy, 0, 0);
            mouse_event((int)MouseEventFlag.LeftUp, dx, dy, 0, 0);
        }
        /// <summary>
        /// 鼠标点击中中键
        /// </summary>
        /// <param name="data"></param>
        public static void MouseMiddleClickEvent(int data)
        {
            GetCursorPos(out POINT pointRecord);
            mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0);
            mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0);
        }

        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }

        }

        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
        }
    }
}
