1

我正在尝试编写一个程序来显示一系列七个图片框(指令、黑色背景、图像、黑色背景等),并且这个系列是由鼠标中键(滚轮)上的 mousedown 事件开始的第一个图片框。对于前四个图片框,该人必须将手指放在方向盘上,并且可能直到第七个。图片框填满屏幕。从盒子到盒子的转换由 winmm.dll 中的 timeGetTime 控制。我很高兴地说,程序的序列部分运行良好。

但是,我有两个问题

  1. 如果中间 mouseup 发生在第五个图片框之前,我需要能够停止此 picBoxes 运行并返回到第一个 picBox。
  2. 我需要记录鼠标向上事件发生在第五个、第六个或第七个框的时间。

然后该人按下鼠标左键或右键,这可以正常工作。一个主要问题似乎是 mouseup 事件不起作用,但是当人们将手指放在左或右键上并让它再次单击时,它会在程序中起作用。

在上一个子程序中的序列之后,我将其分为序列A(picboxes 1-4)和sequenceB(picboxes 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。

4

2 回答 2

1

在我的评论的基础上,我编写了一些代码,它使用单个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
于 2014-02-17T23:06:33.473 回答
0

尽管 Dan 给出了答案,我还是想指出如何处理多个框的鼠标事件。

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

这可以通过更改原始控件的 .Capture 属性来更改。以下代码在鼠标在另一个图片框上按下时创建一个新图片框,并处理来自新图片框的 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

这可能是实现您的具体问题的起点。

于 2014-02-17T23:14:38.860 回答