C# 设置鼠标光标位置

2021-01-24 05:15

阅读:693

标签:point   dllimport   new   ops   enter   primary   return   光标位置   prim   

C# 设置鼠标光标位置

using System.Drawing;
using System.Runtime.InteropServices;

namespace ZB.QueueSys.Common
{
    public class MouseHelper
    {
        private static MouseHelper instance;
        public static MouseHelper Instance
        {
            get
            {
                if (instance == null) instance = new MouseHelper();
                return MouseHelper.instance;
            }
        }

        /// 
        /// 引用user32.dll动态链接库(windows api),
        /// 使用库中定义 API:SetCursorPos 
        /// 
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);
        /// 
        /// 移动鼠标到指定的坐标点
        /// 
        public void MoveMouseToPoint(Point p)
        {
            SetCursorPos(p.X, p.Y);
        }
        /// 
        /// 设置鼠标的移动范围
        /// 
        public void SetMouseRectangle(Rectangle rectangle)
        {
            System.Windows.Forms.Cursor.Clip = rectangle;
        }
        /// 
        /// 设置鼠标位于屏幕中心
        /// 
        public void SetMouseAtCenterScreen()
        {
            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            Point centerP = new Point(winWidth / 2, winHeight / 2);
            MoveMouseToPoint(centerP);
        }

    }
}


调用测试如下:
            int y = Screen.PrimaryScreen.WorkingArea.Height - 180;
            int x = Screen.PrimaryScreen.WorkingArea.Width - 180;
            Point p = new Point(x, y);
            MouseHelper.Instance.MoveMouseToPoint(p);    

  

C# 设置鼠标光标位置

标签:point   dllimport   new   ops   enter   primary   return   光标位置   prim   

原文地址:https://www.cnblogs.com/YYkun/p/12054381.html


评论


亲,登录后才可以留言!