作者: 乔克斯
查看: 2402|回复: 1
打印 上一主题 下一主题

[教程] 【鼠标操作类】常用的通过API操作鼠标定位点击的方法

[复制链接]
跳转到指定楼层
楼主
乔克斯 发表于 2014-8-7 22:14:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看: 2402|回复: 1
    通过Windows API定位鼠标和操作鼠标左右键以及滚轮的几个实用方法,包含了移动鼠标指针到指定位置、控制鼠标滚轮的滚动控制鼠标左右键点击的调用示例(记得要using System.Runtime.InteropServices;):
[C#] 纯文本查看 复制代码
        #region 鼠标API
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="X">目标x坐标</param>
        /// <param name="Y">目标y坐标</param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        const int MMove = 0x0001;      //移动鼠标 
        const int LeftDown = 0x0002; //模拟鼠标左键按下 
        const int LeftUp = 0x0004; //模拟鼠标左键抬起 
        const int RightDown = 0x0008;// 模拟鼠标右键按下 
        const int RightUp = 0x0010;// 模拟鼠标右键抬起 
        const int MiddleDown = 0x0020;// 模拟鼠标中键按下 
        const int MiddleUp = 0x0040;// 模拟鼠标中键抬起 
        const int XDown = 0x0080;
        const int XUp = 0x0100;
        const int Wheel = 0x0800;
        const int VirtualDesk = 0x4000;
        const int Absolute = 0x8000;// 标示是否采用绝对坐标 
        #endregion


/// <summary>
        /// 向下或向上滑动间距(下为负)
        /// </summary>
        /// <param name="x"></param>
        private void BDMouseWheel(int x)
        {
            int TempMY = 0;
            int TempMY2 = 0;
            while(true)
            {
                if (MainForm.StopAll)
                    return;
                OutNum = 60000;
                Thread.Sleep(100);
                if (TempMY == x)
                {
                    return;
                }
                if (x < 0)
                    TempMY2 = new Random().Next(x - TempMY, 0);
                else
                    TempMY2 = new Random().Next(1, x - TempMY);
                mouse_event(Wheel, 0, 0, TempMY2, 0);
                TempMY += TempMY2;
            }
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="EndP">目标坐标</param>
        /// <returns>false即没有到达目的</returns>
        private void MouseMove(Point EndP)
        {
            if (EndP.X > Screen.PrimaryScreen.WorkingArea.Size.Width - 50)
            {
                EndP.X = Screen.PrimaryScreen.WorkingArea.Size.Width - 50;
            }
            if (EndP.Y > Screen.PrimaryScreen.WorkingArea.Size.Height - 50)
            {
                EndP.Y = Screen.PrimaryScreen.WorkingArea.Size.Height - 50;
            }
            if (EndP.X <= 0)
            {
                EndP.X = 50;
            }
            if (EndP.Y <= 0)
            {
                EndP.Y = 50;
            }
            //SetCursorPos
            Point NowMouseP = new Point(Control.MousePosition.X, Control.MousePosition.Y);
            Random Rd = new Random(GetRandomSeed());//0,100
            int spl = 50;
            int spIx = 0;
            int spIy = 0;
            bool Xb = false;
            bool Yb = false;
            int Spx = 0;
            int Spy = 0;
            while (NowMouseP != EndP)
            {
                if (MainForm.StopAll)
                    return;
                OutNum = 60000;
                Thread.Sleep(Rd.Next(20,50));
                NowMouseP = new Point(Control.MousePosition.X, Control.MousePosition.Y);
                Spx = NowMouseP.X - EndP.X;
                Spy = NowMouseP.Y - EndP.Y;
                if (Spx > 0)
                    Xb = true;
                else
                    Xb = false;
                if (Spy > 0) 
                    Yb = true;
                else
                    Yb = false;
                Spx = System.Math.Abs(Spx);
                Spy = System.Math.Abs(Spy);
                if (Spx > Spy && Spy > 0 && Spx > 0)
                    spIx = (Spx * spl) / Spy;
                else if (Spx <= Spy && Spy > 0 && Spx > 0)
                    spIy = (Spy * spl) / Spx;
                else
                {

                }
                SetCursorPos(
                    NowMouseP.X == EndP.X ? NowMouseP.X : (NowMouseP.X + (Xb ? -(Rd.Next(Spx > spIx ? spIx : Spx) + 1) : (Rd.Next(Spx > spIx ? spIx : Spx) + 1)))
                  , NowMouseP.Y == EndP.Y ? NowMouseP.Y : (NowMouseP.Y + (Yb ? -(Rd.Next(Spy > spIy ? spIy : Spy) + 1) : (Rd.Next(Spy > spIy ? spIy : Spy) + 1)))
                    );
            }
        }
        /// <summary>
        /// 随机轴
        /// </summary>
        /// <returns></returns>
        private int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        /// <summary>
        /// 鼠标左键点击
        /// </summary>
        private void ClickMouse()
        {
            mouse_event(LeftDown, 0, 0, 0, 0);
            Thread.Sleep(200);
            mouse_event(LeftUp, 0, 0, 0, 0);
        }


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 转播转播
回复 论坛版权

使用道具 举报

沙发
iHomeSoft 发表于 2014-8-8 10:44:59 | 只看该作者
略屌!略屌!一用到windows api的东西我就歇菜了!
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-11-24 14:13, Processed in 0.534064 second(s), 29 queries , Gzip On.

快速回复 返回顶部 返回列表