描述:
将鼠标向左移动,当光标移动到屏幕左边边框后,鼠标继续向左移动,但光标位置不再变化;
现在希望鼠标继续向左移动时,光标能够从屏幕右侧出现,并继续向左移动;
实现代码如下:
需要声明的变量:
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);