我使用以下代码来处理表单中某些控件的位置;
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,可以将“拖放n”图像拖入其中;
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移动,则放下的图像会随之移动吗?
答案 0 :(得分:0)
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
。
答案 1 :(得分:0)
这是我最终使用的内容。鼠标的感觉要好得多,而且我将其与其他设置一起存储到“设置”中。我认为这是一个不错的解决方案,无需进行任何数据库工作。意见?
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。而且我仍然需要控件上的箭头键功能。