简体   繁体   English

移动光标C#(cursor.position / cursor.clip不起作用)

[英]Moving cursor c# (cursor.position/cursor.clip not working)

I am currently trying to set my cursor in a certain region of the screen. 我目前正在尝试将光标设置在屏幕的特定区域。

I am using the following method: 我正在使用以下方法:

this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50); 
Cursor.Clip = new Rectangle(this.Location, this.Size);

However nothing actually happens when this code is executed. 但是,执行此代码时实际上没有任何反应。 Can anyone tell me what's going on? 谁能告诉我怎么回事?

You shouldn't write this line 你不应该写这行

this.Cursor = new Cursor(Cursor.Current.Handle); //Remove it

Here you set the region where mouse cursor will be. 在此设置鼠标光标所在的区域。 This is should be the first line if you want to "lock" mouse cursor in the rectagle. 如果要在重新标记中“锁定”鼠标光标,这应该是第一行。 If you don't want to do it then comment this line. 如果您不想这样做,请注释此行。

Cursor.Clip = new Rectangle(this.Location, this.Size);

Then you can put cursor in the position 然后可以将光标放在

Cursor.Position = new Point(500, 500);

Small example to show how it can work. 一个小例子,说明它如何工作。

// After first run uncomment this line and you will see mouse "locking" in your form ===> Cursor.Clip = new Rectangle(this.Location, this.Size);
    for (int i = 0; i < 600; i++)
    {
        //Here you move your cursor.
        //We get current position and shift it by 1.
        Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y - 1);
        //Sleep for 100ms
        Thread.Sleep(100);
    }

The cursor should just require cursor.Position to be set without working with handels. 光标只需要cursor.Position即可设置,而无需使用handels。 As long as you don't need to work with global handels this should always work 只要您不需要使用全局处理,就应该始终有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM