winforms 如何移动在运行时创建的PictureBox?

eqoofvh9  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(125)

我在VB.NET中的一个应用程序上工作,我创建了一个PictureBox的副本,并在运行时将其添加到Panel中。我希望在将PictureBox放置在Panel上后,能够使用鼠标移动PictureBox的此副本。我已经尝试处理PictureBox副本的**MouseDown,MouseMove和MouseUp事件,但它似乎不起作用。**这是我使用的代码(当我拖动picturebox1并创建新的picturebox1时):

`Public Sub NewPictureBoxWhenIsDraggedOriginal()
pictureBoxCopy = New PictureBox()
pictureBoxCopy.Image = PictureBox1.Image
pictureBoxCopy.SizeMode = PictureBoxSizeMode.StretchImage
pictureBoxCopy.Size = PictureBox1.Size
pictureBoxCopy.Location = PictureBox1.Location
Panel2.Controls.Add(pictureBoxCopy)
AddHandler pictureBoxCopy.MouseDown, AddressOf Me.PictureBoxCopy_MouseDown
AddHandler pictureBoxCopy.MouseMove, AddressOf Me.PictureBoxCopy_MouseMove
AddHandler pictureBoxCopy.MouseUp, AddressOf Me.PictureBoxCopy_MouseUp
Private Sub PictureBoxCopy_MouseDown(sender As Object, e As MouseEventArgs)
' Set isDragging to True to indicate that the PictureBox is being dragged
isDragging = True

dragOffset = New Point(e.X, e.Y)
End Sub

Private Sub PictureBoxCopy_MouseMove(sender As Object, e As MouseEventArgs)
' Check if the PictureBox is being dragged
If isDragging Then
    ' Update the PictureBox location to follow the mouse cursor
    Dim newLocation As Point = pictureBoxCopy.Location
    newLocation.X += e.X - dragOffset.X
    newLocation.Y += e.Y - dragOffset.Y
    pictureBoxCopy.Location = newLocation
End If
End Sub

Private Sub PictureBoxCopy_MouseUp(sender As Object, e As MouseEventArgs)
' Set isDragging to False to indicate that the PictureBox has been dropped
isDragging = False
End Sub`

字符串
谁能帮助我理解为什么我不能移动PictureBox副本以及如何修复它?任何帮助都将非常感谢。谢谢。
伙计们,我在运行时尝试添加处理程序,但它不起作用,嗯,首先有一个原始的picturebox,(PictureBox1)当我试图拖动它的副本是,我可以拖动(和下降)在面板(面板2),但当我下降,我不能移动副本了,我试图添加处理程序,但我不工作,因为,(我认为处理程序运行在sub上,那是因为不工作),然后我不知道如何使对象(未声明)可以在面板区域移动,因为我不能在代码中说,当鼠标移动未在VS设计器上声明的对象时。

kxe2p93d

kxe2p93d1#

你将PictureBoxCopy的位置设置为与源代码相同,它位于原始Picturebox的顶部。无论如何,你都要将其放入面板中,不要设置位置。下面是我的代码。

Dim isDragging As Boolean
Dim dragOffset As Point

Dim pictureBoxCopy As New PictureBox()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    NewPictureBoxWhenIsDraggedOriginal()
End Sub

Public Sub NewPictureBoxWhenIsDraggedOriginal()

    pictureBoxCopy.Image = PictureBox1.Image
    pictureBoxCopy.SizeMode = PictureBoxSizeMode.StretchImage
    pictureBoxCopy.Size = PictureBox1.Size

    'pictureBoxCopy.Location = PictureBox1.Location

    Panel2.Controls.Add(pictureBoxCopy)

    AddHandler pictureBoxCopy.MouseDown, AddressOf Me.pictureBoxCopy_MouseDown
    AddHandler pictureBoxCopy.MouseMove, AddressOf Me.pictureBoxCopy_MouseMove
    AddHandler pictureBoxCopy.MouseUp, AddressOf Me.pictureBoxCopy_MouseUp

End Sub

Private Sub pictureBoxCopy_MouseDown(sender As Object, e As MouseEventArgs)
    ' Set isDragging to True to indicate that the PictureBox is being dragged
    isDragging = True

    dragOffset = New Point(e.X, e.Y)
End Sub

Private Sub pictureBoxCopy_MouseMove(sender As Object, e As MouseEventArgs)
    ' Check if the PictureBox is being dragged
    If isDragging Then
        ' Update the PictureBox location to follow the mouse cursor
        Dim newLocation As Point = pictureBoxCopy.Location
        newLocation.X += e.X - dragOffset.X
        newLocation.Y += e.Y - dragOffset.Y
        pictureBoxCopy.Location = newLocation
    End If
End Sub

Private Sub pictureBoxCopy_MouseUp(sender As Object, e As MouseEventArgs)
    ' Set isDragging to False to indicate that the PictureBox has been dropped
    isDragging = False
End Sub

字符串

相关问题