【问题标题】:the mouseup event on a picturebox图片框上的 mouseup 事件
【发布时间】:2014-02-18 07:14:38
【问题描述】:

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

但是,我有两个问题

  1. 我需要能够停止运行 picBox 并返回到 如果中间 mouseup 发生在第五个之前,则第一个 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。

【问题讨论】:

  • TL;DR 但您将需要一个或多个模块级变量来跟踪鼠标更改的时间。如果图片框没有填满表单,您还必须监视表单的鼠标事件。
  • 如果你的 mousedown 没有在 picBox2 上发生,你将无法在 picBox2 上处理 mouseup 等。为什么不使用单个 PictureBox 并更改图片?
  • @DanVerdolino:您实际上可以做到这一点。看我的回答。

标签: vb.net events picturebox mouseup


【解决方案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

【讨论】:

    【解决方案2】:

    尽管 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
    

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

    【讨论】:

    • 好主意。他还需要为下一次运行的新图片框添加鼠标按下处理程序。
    • 是的,这个答案一般来说更像是一个原则证明。无论如何,一个 PB 的解决方案可能会更可取。但答案在其他应用程序中也很重要(例如,从一个控件到另一个控件的自定义拖放)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多