【问题标题】:Move PictureBox with Arrow Keys?用箭头键移动图片框?
【发布时间】:2018-11-13 05:07:08
【问题描述】:

我使用以下代码来处理表单中某些控件的定位;

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    'Sub detects which arrow key is pressed
    Dim strControlName As String
    ' Get the name of the control
    strControlName = Me.ActiveControl.Name
    Dim aControl = Me.Controls.Item(strControlName)
    If strControlName <> "PrintButton" Then
        If keyData = Keys.Up Then
            aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
            Return True
        End If
        'detect down arrow ke
        If keyData = Keys.Down Then
            aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
            Return True
        End If
        'detect left arrow key
        If keyData = Keys.Left Then
            aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
            Return True
        End If
        'detect right arrow key
        If keyData = Keys.Right Then
            aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
            Return True
        End If
    End If
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

我还有一个允许将图像拖放到其中的 PictureBox;

Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
    Dim picbox As PictureBox = CType(sender, PictureBox)
    Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())

    If files.Length <> 0 Then
        Try
            picbox.Image = Image.FromFile(files(0))
            pbSig.ImageLocation = files(0)
        Catch ex As Exception
            MessageBox.Show("Problem opening file ")
        End Try
    End If
End Sub

Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub

有没有一种方法可以使 PictureBox 使用箭头键“可移动”?我不能在表单上使用 KeyPress 事件,因为我已经在其他地方使用它。我希望我可以将焦点放在 PictureBox 上或允许用户执行“+箭头”事件。

另外,如果我让 PictureBox 移动,丢弃的图像会随之移动吗?

【问题讨论】:

  • 您的代码工作正常。您只有一个问题:PictureBox 不能成为活动控件(不使用默认类样式,它需要ControlStyles.Selectable)。如果您将 aControl.Location = (...) 更改为 PictureBox 的名称(例如,pbSig.Location = (...),那么 PictureBox 将移动并接受放置(假设您在某处有 set pbSig.AllowDrop = True),设置新图像。您在 @ 中有错字987654327@ => pbSig.ImageLocation = files(0) 应该是 picbox.ImageLocation = files(0)

标签: vb.net


【解决方案1】:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Up Then
        PictureBox1.Top -= 5
    End If
    If e.KeyCode = Keys.Down Then
        PictureBox1.Top += 5
    End If
    If e.KeyCode = Keys.Left Then
        PictureBox1.Left -= 5
    End If
    If e.KeyCode = Keys.Right Then
        PictureBox1.Left += 5
    End If
End Sub

您可以使用此代码通过箭头键移动PictureBox

【讨论】:

  • OP 已经覆盖了ProcessCmdKey,在这种情况下这是正确的做法(其中之一),因为当自动重复是一个选项时,它将允许正确处理键。
  • 我还说我不能使用 KeyPress 事件,因为我已经在其他地方使用它了。除非...
  • @Mgfranz :作为记录,您可以将多个事件处理程序附加到单个事件。在 VB.NET 中,您要么使用 AddHandler,要么(在这种情况下)创建另一个方法并确保它上面有 Handles Me.KeyDown。方法的实际名称并不重要。如果您愿意,可以将其命名为 Form1_KeyDown_2MyHappyBanana
  • @Vincent,这正是我的想法。只是制作另一个事件处理程序来处理 KeyDown。谢谢。
【解决方案2】:

这是我最终使用的。鼠标的感觉要好得多,而且我可以将它与其他设置一起存储到设置中。我认为这是一个很好的解决方案,无需进行任何数据库工作。意见?

Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
    pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
    pbSig.AllowDrop = True
End Sub

End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
    startX = MousePosition.X
    startY = MousePosition.Y
    mDown = True
    valX = False
    valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
    'Check if mouse=down
    If mDown = True Then
        endX = (MousePosition.X - Me.Left)
        endY = (MousePosition.Y - Me.Top)
        If valY = False Then
            startY = endY - sender.top
            valY = True
        End If
        If valX = False Then
            startX = endX - sender.left
            valX = True
        End If
        sender.left = endX - startX
        sender.top = endY - startY
    End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
    My.Settings.pbSigLoc = pbSig.Location
    mDown = False
    valX = False
    valY = False
End Sub

这样,用户需要做的就是使用鼠标定位 pB 及其内容,我不需要再次调用 ProcessCmdKey。而且我仍然在我需要的控件上具有箭头键功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多