注册 登录
编程论坛 VB.NET论坛

VB .NET 鼠标指针自动移动问题

ZMGTCDY 发布于 2013-07-20 17:06, 2932 次点击
有两个控件Label1和Label2,如何做到让鼠标指针按一定时间间隔自动在两个控件之间移动(如,在每个控件停留2秒再跳到另一个控件上如此往复)?要代码。请各位大师帮忙。谢谢!
4 回复
#2
不说也罢2013-07-21 17:30
程序代码:
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-7-21 17:32 编辑 ]
#3
ZMGTCDY2013-07-22 21:05
回复 2楼 不说也罢
看到你的代码我很兴奋!这几天一直在网上摸爬滚打,可是相关的叙述不多,找了一点并用了自己的一些土办法效果不理想。你的代码让我眼前一亮,而且完全达到了我的预期效果。看得出你是个很严谨的人事先进行了测试,非常完整。不知道用什么语言感谢你,想了许久…感恩!
#4
不说也罢2013-07-23 12:21
互相学习,共同提高。
#5
fajoker2013-08-14 11:41
应该再一个timer控件,WIN7+VS2010中测试无法停止测试不知道是什么原因,点击Button1单击事件无响应。
1