图片框上的mouseup事件

时间:2014-02-17 19:02:49

标签: vb.net events picturebox mouseup

我正在尝试编写一个程序,显示一系列七个图片框(说明,黑色背景,图像,黑色背景等),这个系列是由鼠标中键(滚轮)上的mousedown事件启动的)在第一个图片框上。这个人必须至少在前四个图片盒上保持他们的手指,直到第七个。图片框填满了屏幕。从一个框到另一个框的转换由winmm.dll中的timeGetTime控制。 我很高兴地说,该计划的序列部分非常有效。

但是,我有两个问题

  1. 我需要能够阻止这个picBoxes的运行并返回到 如果中间鼠标在第五个之前发生,则第一个picBox 图片框。
  2. 我需要记录鼠标时间事件的时间 它发生在第五,第六或第七个盒子里。
  3. 然后,该人按下鼠标左键或右键,这样就可以了。 一个主要问题似乎是mouseup事件无法正常工作,但是当用户将手指放在左侧或右侧按钮并让它再次点击时,它会在程序中稍后运行。

    在前一个子程序中的序列之后,我将其划分为sequenceA(picbox 1-4)和sequenceB(picbox 5-7)。我已经把:

    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上运行,但我需要知道picbox 2-4。

2 个答案:

答案 0 :(得分:1)

根据我的评论,我编写了一些使用单个PictureBox显示所有图片的代码,这样就可以处理MouseUpMouseDown

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 :(得分:0)

尽管Dan的回答我想指出如何处理多个盒子的鼠标事件。

问题是,当您在控件上按下鼠标按钮时,鼠标会被捕获到此控件内部。因此,只要没有再次释放按钮,就只处理来自此特定控件的鼠标事件。

可以通过更改原始控件的.Capture属性来更改此设置。 当在另一个图片框上按下鼠标并从新图片框处理鼠标事件时,以下代码会创建一个新的图片框。

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

这可能是实施具体问题的起源点。