How do I clear Cursor.Clip in C# and allow the cursor to move freely again?

风流意气都作罢 提交于 2019-12-10 19:39:39

问题


I am trying to lock the cursor into the form, this is for a mouse locker application, I am trying to dispose the cursor so it will reset the Cursor.Clip when they unlock it.

So far I have:

Cursor.Clip = new Rectangle(x +8, y +30, Size.Width -16, Size.Height -38);

That works fine.

But I cannot figure out how to clear the clip when they unlock it. I have tried Cursor.Dispose(); But that doesn't work.

Any ideas? Thanks.


回答1:


Set Clip to a Rectangle that contains the screen's dimensions.

Cursor.Clip = Screen.PrimaryScreen.Bounds;

Of course, this won't work with dual monitor setups, but you get the idea.




回答2:


actually the idea is to just set it to a new rectangle

Cursor.Clip = new Rectangle();

It works regardless of the situation.




回答3:


Try this: when your application starts, get the value of Cursor.Clip and save it as the unclipped value. Then when you want to reset the clip, assign the unclipped value.

UPDATE: In this page it says that to unclip the cursor in VB.NET, it is enough to do Cursor.Clip=Nothing. But this is strange since Rectangle is a structure and thus it can't be set to null. So in C#, maybe it would be Cursor.Clip=Rectangle.Empty or Cursor.Clip=default(Rectangle)?



来源:https://stackoverflow.com/questions/1805260/how-do-i-clear-cursor-clip-in-c-sharp-and-allow-the-cursor-to-move-freely-again

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!