I have 2 screens connected to my computer. I also have a HotKey control that can trigger a function call in my application even when it is not focused.

I need to do the following. When I start my C# application, my mouse should be locked only to the current screen (meaning that it shouldn't be able to go on to the other screen). On pressing the HotKey, it should be get locked onto the second screen and not be able to move on the first screen. So basically the HotKey (essentially a function call) should allow toggling between the two screens and restricting mouse movement only to that screen.

Is there any way I can accomplish this?

有帮助吗?

解决方案

You can use Cursor.Clip property. E.g.Cursor.Clip = Screen.PrimaryScreen.WorkingArea

Without WinForms you can use ClipCursor function:

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool ClipCursor(ref RECT rcClip);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top