Vb.net picturebox上的鼠标移动事件
我正试图编写一个程序,显示一系列七个图片框(说明、黑色背景、图像、黑色背景等),这个系列是由第一个图片框上鼠标中键(滚轮)上的鼠标向下事件开始的。至少在前四个图片框中,用户必须将手指放在方向盘上,可能一直到第七个。图片框填满了屏幕。从box到box的转换由winmm.dll中的timeGetTime控制。 我很高兴地说,程序的顺序部分工作得很好 然而,我有两个问题Vb.net picturebox上的鼠标移动事件,vb.net,events,picturebox,mouseup,Vb.net,Events,Picturebox,Mouseup,我正试图编写一个程序,显示一系列七个图片框(说明、黑色背景、图像、黑色背景等),这个系列是由第一个图片框上鼠标中键(滚轮)上的鼠标向下事件开始的。至少在前四个图片框中,用户必须将手指放在方向盘上,可能一直到第七个。图片框填满了屏幕。从box到box的转换由winmm.dll中的timeGetTime控制。 我很高兴地说,程序的顺序部分工作得很好 然而,我有两个问题 我需要能够停止运行Picbox并返回到 如果中间鼠标在第五个鼠标之前出现,请选择第一个picBox 图片盒 我需要记录鼠标移动事件的
Private Property sequenceA As Boolean
Private Property sequenceB As Boolean
Private Sub picBox2_mouseup(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picBox2.MouseUp
If MouseButtons.Middle Then
If sequenceA = True Then
picBox1.Visible = True
sequenceB = False
sequenceA = False
End If
End If
End Sub
我已经试了好几天了!上面的代码,如果有效的话,只会告诉我鼠标是否在picBox2上移动,但我需要知道picBox2-4的情况。基于我的评论,我编写了一些代码,使用一个
PictureBox
来显示所有的图像,这允许MouseUp
和MouseDown
进行处理
Public Class Form1
Private currentImageIndex As Integer
Private images As List(Of Bitmap)
Private loopTimer As Threading.Timer
Private timeForEachImage As Long = 500 ' ms
Private stopTime As DateTime
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Middle Then
stopLoop()
Select Case currentImageIndex
Case 0 To 3 ' stopped before the 5th
changePictureIndex(0)
Case 4, 5, 6 ' on or after the 5th
stopTime = DateTime.Now
End Select
End If
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Middle Then startLoop()
End Sub
Private Sub startLoop()
stopLoop()
currentImageIndex = 0
loopTimer.Change(0, timeForEachImage)
End Sub
Private Sub stopLoop()
loopTimer.Change(Threading.Timeout.Infinite, Threading.Timeout.Infinite)
End Sub
Private Sub imageTimerCallback()
currentImageIndex = Math.Min((currentImageIndex + 1), 7)
If currentImageIndex < 7 Then changePictureIndex(currentImageIndex)
End Sub
Private Sub changePictureIndex(ByVal index As Integer)
If PictureBox1.InvokeRequired Then
PictureBox1.Invoke(New Action(Of Integer)(AddressOf changePictureIndex), index)
Else
PictureBox1.Image = images(index)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
images = New List(Of Bitmap)
images.Add(New Bitmap("C:\...\img1.png")) ' load all your images in order
images.Add(New Bitmap("C:\...\img2.jpg")) ' etc.
loopTimer = New Threading.Timer(AddressOf imageTimerCallback)
End Sub
End Class
公共类表单1
私有currentImageIndex为整数
私有图像作为列表(位图)
作为线程的私有loopTimer.Timer
专用timeForEachImage的长度=500'毫秒
作为日期时间的私有停止时间
私有子PictureBox1\u MouseUp(发送方作为对象,e作为MouseEventArgs)处理PictureBox1.MouseUp
如果e.Button=Windows.Forms.MouseButtons.Middle,则
停止循环()
选择案例currentImageIndex
案例0到3'在第5个之前停止
changePictureIndex(0)
案例4、5、6'在第五个或之后
stopTime=DateTime.Now
结束选择
如果结束
端接头
私有子PictureBox1\u MouseDown(发送方作为对象,e作为MouseEventArgs)处理PictureBox1.MouseDown
如果e.Button=Windows.Forms.MouseButtons.Middle,则为startOOP()
端接头
私有子对象
停止循环()
currentImageIndex=0
loopTimer.Change(0,timeForEachImage)
端接头
专用子stopLoop()
loopTimer.Change(Threading.Timeout.Infinite,Threading.Timeout.Infinite)
端接头
专用子映像TimerCallback()
currentImageIndex=Math.Min((currentImageIndex+1),7)
如果currentImageIndex<7,则更改图片索引(currentImageIndex)
端接头
私有子changePictureIndex(ByVal索引为整数)
如果需要PictureBox1.invoke,则
调用(整数的新操作)(changePictureIndex的地址),索引)
其他的
PictureBox1.Image=图像(索引)
如果结束
端接头
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
图像=新列表(位图)
添加(新位图(“C:\…\img1.png”)“按顺序加载所有图像
添加(新位图(“C:\…\img2.jpg”))等。
loopTimer=New Threading.Timer(imageTimerCallback的地址)
端接头
末级
尽管Dan给出了答案,我还是想指出如何处理多个框的鼠标事件
问题是,当您按下控件上的鼠标按钮时,鼠标会在该控件内被捕获。因此,只要按钮没有再次释放,就只能处理来自此特定控件的鼠标事件
这可以通过更改原始控件的.Capture属性来更改。
当鼠标按下另一个picturebox时,以下代码将创建一个新的picturebox,并处理来自新picturebox的mouseup事件
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
Dim pb2 As New PictureBox 'Create new picturebox
With pb2
.Name = "TheNewPicturebox"
.BackColor = Color.Green 'Make it more visible
.Size = PictureBox1.Size 'Set Size and Location over the old pb
.Location = PictureBox1.Location
AddHandler .MouseUp, AddressOf p2_MouseUp 'Add a handler for the MouseUp-event
End With
PictureBox1.Capture = False 'IMPORTANT: Releases the mouse from the old picturebox
Me.Controls.Add(pb2) 'Add the new pb to the form
pb2.BringToFront() 'Places the new control over the old one (z-order)
End Sub
Private Sub p2_MouseUp(sender As Object, e As MouseEventArgs)
MessageBox.Show("Greetings from " & CType(sender, PictureBox).Name)
End Sub
这可能是实现具体问题的起点。TL;但是您需要一个或多个模块级变量来跟踪鼠标更改的时间。如果图片框无法填充表单,您还必须监视表单的鼠标事件。如果您的鼠标未在picBox2上落下,您将无法处理picBox2上的鼠标,等等。为什么不使用一个PictureBox并更改图片?@DanVerdolino:您实际上可以做到这一点。看看我的答案,好主意。他还需要为下一次运行的新图片框添加鼠标按下处理程序。是的,这个答案更像是一个原则证明。无论如何,一个含有一个PB的解决方案可能更可取。但答案在其他应用程序中也很重要(例如,从一个控件到另一个控件的自定义拖放)。