如何在 winforms 中释放捕获(剪切)的光标?

How to release captured (clipped) cursor in winforms?

我试图用 IronPython 实现 this example。 我使用此方法成功捕获了光标:

def MoveCursor(self):
    #Set the Current cursor, move the cursor's Position,
    #and set its clipping rectangle to the form.
    if self.MouseLocked == False:
        self.Cursor = System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle)
        System.Windows.Forms.Cursor.Position = System.Drawing.Point(self.Cursor.Position.X - 50, self.Cursor.Position.Y - 50)
        System.Windows.Forms.Cursor.Clip = System.Drawing.Rectangle(self.Location, self.Size)
        self.MouseLocked = True
    elif self.MouseLocked == True:
        self.Cursor = System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle)
        #System.Windows.Forms.Cursor.Position = System.Drawing.Point(self.Cursor.Position.X - 50, self.Cursor.Position.Y - 50)
        System.Windows.Forms.Cursor.Clip = None
        self.MouseLocked = False

我尝试使用 class 变量来切换捕获,但它没有像我预期的那样工作。 如何释放鼠标?我需要使用哪种方法或事件?

更新: 我尝试使用 self.Cursor.Dispose(),但这只是让我的光标不可见 :D,有点有趣,但不是我需要的。

"松开" 光标的截断从技术上讲叫做"清除",会得到更好的搜索结果。网上有 许多 个其他示例(here 是一个)在专门的 C# 中处理这个确切的问题,并且用 ironpython 做同样的事情也没有什么不同。

当您查看 the documentation 时,第一句话特别是 隐含地 回答了您的问题(强调我的):

The following code example creates a cursor from the Current cursor's Handle, changes its position and clipping rectangle.

清除之前设置的位置和裁剪矩形,需要将Clip属性设置为, 空 Rectangle:

Cursor.Clip = new Rectangle();

这有效地清除了光标的位置和裁剪矩形。你的特殊情况会将它放在你的 elif 块中:

elif self.MouseLocked == True:
    ...
    System.Windows.Forms.Cursor.Clip = new System.Drawing.Rectangle()
    ...