public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(Int32 left, Int32 top, Int32 right, Int32 bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
}
[DllImport("user32.dll")]
static extern bool ClipCursor(IntPtr lpRect);
[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern bool ClipCursor(ref RECT lpRect);
public static void LimitMouse(int left, int top, int right, int bottom)
{
var rect = new RECT(left, top, right, bottom);
bool flag = ClipCursor(ref rect);
}
public static void LimitMouse()
{
bool flag = ClipCursor(IntPtr.Zero);
}
以上方法测试,可用,但是有一个问题,当使用双屏显示器的时候,设置的鼠标范围,超出一块屏幕的位置时,也就是在副屏的范围,该功能变失效了。
本文探讨了一个使用C#实现的限制鼠标活动区域的代码片段,该功能在单显示器环境下正常工作,但在双屏显示设置中,当指定的范围超出主屏幕时,功能失效。问题主要出现在多显示器配置下,代码无法正确约束鼠标在指定屏幕内。针对这个问题,文章可能涉及显示器坐标系统、Windows API调用以及多显示器环境的适配策略。
2118

被折叠的 条评论
为什么被折叠?



