在运行时创建和移动图片框

时间:2014-12-15 02:00:50

标签: vb.net

我正在使用VB 2010

我正在尝试创建一个程序,在运行期间我可以使用mousedown创建一个新的图片框,然后可以移动创建的每个图片框。我觉得好像我很亲近,有人可以帮助我吗?

Private Sub PictureBox2_Mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.Click
    Dim newPictureBox As New PictureBox

    newPictureBox.Image = Image.FromFile("C:\Users\Blair\Desktop\table8.jpg")
    newPictureBox.Visible = True
    newPictureBox.Top = 0
    newPictureBox.Width = 200
    newPictureBox.Height = 200
    newPictureBox.Left = 100
    newPictureBox.BringToFront()
    newPictureBox.SizeMode = PictureBoxSizeMode.StretchImage

    If IsDragging Then
        Dim EndPoint As Point = newPictureBox.PointToScreen(New Point(e.X, e.Y))
        IsClick = False
        newPictureBox.Left += (EndPoint.X - StartPoint.X)
        newPictureBox.Top += (EndPoint.Y - StartPoint.Y)
        StartPoint = EndPoint
        LastPoint = EndPoint
    End If


    'add control to form
    Controls.Add(newPictureBox)

End Sub

问题:图片框填充但是它无法移动/拖动mousedown

2 个答案:

答案 0 :(得分:1)

这可能或者可能不是您正在寻找的,因为它没有使用您所有的原始变量,但我尝试创建一个简单的示例供您学习。要测试这个只是创建一个新的空白表单(Form1)而不添加任何控件只需将此代码复制/粘贴到表单代码的顶部并运行它。

如果单击表单上的任意位置,它将创建一个新的图片框,只要按住鼠标左键,就可以用鼠标移动它。当您松开鼠标左键时,它将停止移动图片框,您可以:

a)单击表单上的另一个空白区域以创建新的图片框并将其移动到您想要的位置,或者

b)您可以点击之前创建的现有图片框之一并将其移至其他位置。

包含评论以帮助您更好地了解其工作原理。

Public Class Form1
    Dim oDragPoint As Point = Nothing
    Dim oCurrentPictureBox As PictureBox = Nothing

    Private Sub Event_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        If TypeOf sender Is PictureBox Then
            ' Move existing picturebox
            oCurrentPictureBox = sender
            oCurrentPictureBox.BringToFront()
            oDragPoint = New Point(e.X, e.Y)
        Else
            ' Create a new picturebox
            oCurrentPictureBox = New PictureBox

            oCurrentPictureBox.Image = Image.FromFile("C:\Users\Blair\Desktop\table8.jpg")
            oCurrentPictureBox.Location = Me.PointToClient(Windows.Forms.Cursor.Position)
            oCurrentPictureBox.Width = 200
            oCurrentPictureBox.Height = 200
            oCurrentPictureBox.SizeMode = PictureBoxSizeMode.StretchImage

            ' Add events to the new picturebox we just created so that it can be moved again later
            AddHandler oCurrentPictureBox.MouseDown, AddressOf Event_MouseDown
            AddHandler oCurrentPictureBox.MouseMove, AddressOf Event_MouseMove
            AddHandler oCurrentPictureBox.MouseUp, AddressOf Event_MouseUp

            ' Add picturebox to form
            Me.Controls.Add(oCurrentPictureBox)

            ' Bring picturebox to front after it has been added to the form to ensure it is on top of all other controls in the controls collection
            oCurrentPictureBox.BringToFront()
        End If
    End Sub

    Private Sub Event_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        If oCurrentPictureBox IsNot Nothing Then
            ' Move picture box wherever the mouse moves
            Dim oMouseCursorPoint As Point = Me.PointToClient(Windows.Forms.Cursor.Position)
            oCurrentPictureBox.Location = New Point(oMouseCursorPoint.X - oDragPoint.X, oMouseCursorPoint.Y - oDragPoint.Y)
        End If
    End Sub

    Private Sub Event_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        ' Drop picturebox and stop moving it around
        oCurrentPictureBox = Nothing
        oDragPoint = Nothing
    End Sub
End Class

答案 1 :(得分:0)

很好的答案示例。 在动态创建时,一旦你在创建对象之后的任何地方添加了所需的句柄,即picturebox1_click-.click场景,你就可以做任何你想做的事情。因此,如果您希望图片框可点击并可移动,请使用事件处理程序 像这样 public sub picturebox_click(发件人作为对象,e作为eventargs) 当用户点击图片框时,做任何你想做的事情。 dim p as picturebox = trycast(sender,picturebox) 从这里你可以搞定它。当用户点击p =发件人等时 这样,无论他们点击什么图像都会移动或做任何事情,向用户显示他们在要点击的图像上你可以添加borderstyle borderstyle = bordrstyle.3dfixed作为示例然后在你创建的点击事件中你有borderstyle = borderstyle.none这让他们知道图像在鼠标下。无论如何,我现在正在做一些事情这样做,所以想分享我发现的Learnerguy

这是我正在使用的一个例子  昏暗的平底锅作为新面板         pan.Name =" Panel" &安培; Convert.ToString(计数)

    pan.BackColor = Color.Black
    AddHandler pan.MouseEnter, AddressOf pan_MouseEnter
    AddHandler pan.Click, AddressOf pan_Click
    AddHandler pan.MouseDown, AddressOf pan_MouseDown
    AddHandler pan.MouseMove, AddressOf pan_MouseMove
    AddHandler pan.MouseUp, AddressOf pan_MouseUp

    Form2.Panel1.Controls.Add(pan)
    Dim gdonopen As New OpenFileDialog
    gdonopen.ShowDialog
    pan.BackgroundImage = Image.FromFile(gdonopen.FileName)
    pan.BackgroundImageLayout = ImageLayout.Stretch
    objCtrl = pan

添加所需的事件,这将为您提供可点击的可移动面板开关面板到图片框。 享受你的同事程序员吧! -Learnerguy