如果要创建非常安全的Windows应用程序,使用的一项重要技术就是将光标置于特定的控件上,等待用户下次单击。
1、添加个Module页面Module1.vb来放置公共方法或属性。
2、在Module1.vb页面中添加如下公共方法,代码如下:
- Public Sub SnapToControl(ByVal Control As Control)
- ' Snaps the cursor to the bottom middle of the passed control
- Dim objPoint As Point = Control.PointToScreen(New Point(0, 0))
- objPoint.X += (Control.Width / 2)
- objPoint.Y += ((Control.Height / 4) * 3)
- Cursor.Position = objPoint
- End Sub
然后再 Form1_Load下指定要把光标置于哪控件上,代码如下:
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- SnapToControl(TextBox1) '将光标置于TextBox1上
- 'SnapToControl(TextBox2) 将光标置于TextBox2上
- 'SnapToControl(Button1) 将光标置于Button1上
- 'SnapToControl(CheckBox1) 将光标置于CheckBox1上
- End Sub