API函数配合鼠标钩子
[DllImport("user32.dll")]
public static extern bool ClipCursor(Rectangle rect);//限定鼠标区域
此函数可以限定鼠标区域,但当程序失去焦点时就失效了,想要全局限定鼠标区域,得配合全局鼠标钩子来实现。
下面的代码可以把鼠标全局的限定在主显示器内:
void mh_MouseMoveEvent(object sender, MouseEventArgs e)
{
Screen[] sc=Screen.AllScreens;
int x = e.Location.X;
int y = e.Location.Y;
if (x>sc[0].Bounds.Height)
{
x = sc[0].Bounds.Height;
}
if (y>sc[0].Bounds.Width)
{
y = sc[0].Bounds.Width;
}
Rectangle rec = sc[0].Bounds;
ClipCursor(rec);
}
“mh_MouseMoveEvent”为鼠标钩子事件。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。