如何让鼠标不离开我想要的矩形?

时间:2013-04-30 20:42:36

标签: mouse vb.net-2010

所以我尝试了

cursor.Clip = New Rectangle (desired coordinates)

但由于某种原因,它似乎不起作用。我不知道我做错了什么。它适用于两侧(右侧和底部),但它不适用于另一侧。但即使在左侧,它也不在右侧坐标上,我设置了矩形。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

确保Rectangle的值位于屏幕坐标中。例如,以下代码将在单击Button1时将光标剪切到Button2:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Cursor.Clip = Me.RectangleToScreen(Button2.Bounds) ' confine cursor to the second button
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Cursor.Clip = Nothing ' release the clip
    End Sub

End Class