C# 鼠标光标到达屏幕边缘后从另一边缘出现

本文介绍了一种实现鼠标在多屏环境下无缝移动的方法。通过检测鼠标位置并使用SetCursorPos函数,在鼠标达到屏幕边缘时将其传送到另一侧对应位置,从而创造出平滑的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述:

将鼠标向左移动,当光标移动到屏幕左边边框后,鼠标继续向左移动,但光标位置不再变化;

现在希望鼠标继续向左移动时,光标能够从屏幕右侧出现,并继续向左移动;

实现代码如下:

需要声明的变量:

    bool isPrimary = true;   //是否是主屏
    int primaryScreenWidth = 0;  //主屏
    int primaryScreenHeight = 0;
    int secondScreenWidth = 0;    //副屏
    int secondScreenHeight = 0;

需要的的函数如下:

   //鼠标到屏幕最左或最右端
        [DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);

        //设置屏幕大小
        private void SetScreenSize()
        {
            primaryScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            primaryScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            //判断是否不止一个屏幕
            if (System.Windows.Forms.Screen.AllScreens.Length > 1)
            {
                secondScreenWidth = System.Windows.Forms.Screen.AllScreens[1].WorkingArea.Width;
                secondScreenHeight = System.Windows.Forms.Screen.AllScreens[1].WorkingArea.Height;
            }
        }

        //判断光标是否在主屏幕上
        private void SetScreenState()
        {
            isPrimary = true;
            //获取当前鼠标光标位置
            int posX = System.Windows.Forms.Cursor.Position.X;
            int posY = System.Windows.Forms.Cursor.Position.Y;
            // 判断当前是哪个屏幕
            if (posX > primaryScreenWidth)
            {
                isPrimary = false;
            }
        }

        //获取光标位置
        private void GetCursorPos()
        {
            int posX = System.Windows.Forms.Cursor.Position.X;
            int posY = System.Windows.Forms.Cursor.Position.Y;
            if (isPrimary)
            {
                if (posY <= 10)
                {
                    ResetCursorPos(true);
                }
                if (posY >= primaryScreenHeight - 10)
                {
                    ResetCursorPos(false, true);
                }
                if (posX <= 10)
                {
                    ResetCursorPos(false, false, true);
                }
                if (posX >= primaryScreenWidth - 10)
                {
                    ResetCursorPos(false, false, false, true);
                }
            }
            else
            {
                if (posY <= 10)
                {
                    ResetCursorPos(true);
                }
                if (posY >= secondScreenHeight - 10)
                {
                    ResetCursorPos(false, true);
                }
                if (posX <= primaryScreenWidth + 10)
                {
                    ResetCursorPos(false, false, true, false);
                }
                if (posX >= primaryScreenWidth + secondScreenWidth - 10)
                {
                    ResetCursorPos(false, false, false, true);
                }
            }
        }
            
        //重新设置光标的位置
        private void ResetCursorPos(bool isTop=false, bool isBottom=false, bool isLeft=false, bool isRight=false)
        {
            int posX = System.Windows.Forms.Cursor.Position.X;
            int posY = System.Windows.Forms.Cursor.Position.Y;
            if (isPrimary)
            {
                if (isTop)
                {
                    SetCursorPos(posX, primaryScreenHeight-11);   //上→下
                }
                if (isBottom)
                {
                    SetCursorPos(posX, 11);   //下→上
                }
                if (isLeft)
                {
                    SetCursorPos(primaryScreenWidth-11, posY);   //左→右
                }
                if (isRight)
                {
                    SetCursorPos(11, posY);   //右→左
                }
            }
            else
            {
                if (isTop)
                {
                    SetCursorPos(posX, secondScreenHeight-11);   //上→下
                }
                if (isBottom)
                {
                    SetCursorPos(posX, 11);   //下→上
                }
                if (isLeft)
                {
                    SetCursorPos(primaryScreenWidth+secondScreenWidth-11, posY);   //左→右
                }
                if (isRight)
                {
                    SetCursorPos(primaryScreenWidth+11, posY);   //右→左
                }
            }
        }
这里的10和11表示:在距离屏幕边缘10像素的时候,就把光标移动到另一端11像素的地方;

Tip:

 private static extern int SetCursorPos(int x, int y);

 函数功能:该函数把光标移到屏幕的指定位置。如果新位置不在由 ClipCursor函数设置的屏幕矩形区域之内,则系统自动调整坐标,使得光标在矩形之内。

函数原型:BOOL SetCursorPOS(int X,int Y);

参数:

X:指定光标的新的X坐标,以屏幕坐标表示。

Y:指定光标的新的Y坐标,以屏幕坐标表示。

返回值:如果成功,返回非零值;如果失败,返回值是零,若想获得更多错误信息,请调用GetLastError函数。

备注:该光标是共享资源,仅当该光标在一个窗口的客户区域内时它才能移动该光标。

C#中使用该函数首先导入命名空间:

using System.Runtime.InteropServices;  

然后写API引用部分的代码,放入 class 内部:

[DllImport("user32.dll", EntryPoint = "SetCursorPos")]  
private static extern int SetCursorPos(int x, int y);  

这个函数有两个个参数,第一个参数是指定光标的新的X坐标;第二个参数是指定光标的新的Y坐标。例如:

SetCursorPos(100, 100);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独立游戏开发指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值