VB .NET 鼠标指针自动移动问题
有两个控件Label1和Label2,如何做到让鼠标指针按一定时间间隔自动在两个控件之间移动(如,在每个控件停留2秒再跳到另一个控件上如此往复)?要代码。请各位大师帮忙。谢谢!
2013-07-20 17:06
程序代码:Imports System.Collections.Generic
Imports System.Drawing
Imports System.Runtime.InteropServices
'窗体上任意拉出两个标签控件,一个按钮控件
'测试环境 winXP + .NET 2008
'其它环境未测试过
Public Class Form1
<DllImport("User32")> _
Public Shared Sub SetCursorPos(ByVal x As Integer, ByVal y As Integer)
End Sub
Private Mouse1 As New Point(1, 1)
Private Mouse2 As New Point(1, 1)
Private p As New Point(1, 1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Show()
Me.AcceptButton = Button1
Button1.Text = "停止测试"
Timer1.Interval = 2000
Mouse1 = Label1.PointToScreen(p)
Mouse2 = Label2.PointToScreen(p)
Timer1.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "停止测试" Then
Timer1.Stop()
Button1.Text = "开始测试"
Else
Timer1.Start()
Button1.Text = "停止测试"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer = 0
i += 1
If i Mod 2 <> 0 Then
SetCursorPos(Mouse1.X + Label1.Width / 2, Mouse1.Y + Label1.Height / 2)
Else
SetCursorPos(Mouse2.X + Label2.Width / 2, Mouse2.Y + Label2.Height / 2)
End If
End Sub
Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
'窗体移动时重新获取坐标
Mouse1 = Label1.PointToScreen(p)
Mouse2 = Label2.PointToScreen(p)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'窗体调整大小时,重新获取坐标
Mouse1 = Label1.PointToScreen(p)
Mouse2 = Label2.PointToScreen(p)
End Sub
End Class

2013-07-21 17:30
2013-07-22 21:05

2013-07-23 12:21
2013-08-14 11:41