Creating new dragable images

LacksdaSKILLZ

Member
Joined
Sep 27, 2012
Messages
6
Programming Experience
Beginner
Hey all.

OK so straight to the point. I have a picture dragging class called myPictureBox:

VB.NET:
Imports System.Windows.Forms
Imports System.Drawing
Public Class myPictureBox
    'This picture box is a bit special since you can change the border of the Picturebox.
    'Notice that in the default picture box, you don't have too many options to customize
    'the border. In this class, you just override the OnPaint event and draw the
    'appropriate border when you click on any image control.
    'I wanted to do this, since I had multiple images in a page, and I wanted to change
    'the border or something when you click the image
    Inherits PictureBox
    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        If Me.BackColor = Color.Transparent Then
            ControlPaint.DrawBorder(pe.Graphics, pe.ClipRectangle, Color.Transparent, ButtonBorderStyle.None)
        Else
            ControlPaint.DrawBorder(pe.Graphics, pe.ClipRectangle, Color.Green, ButtonBorderStyle.Dashed)
        End If
        MyBase.OnPaint(pe)
    End Sub

    Private Sub myPictureBox_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        Me.BackColor = Color.Transparent
    End Sub

    Private Sub myPictureBox_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        If Me.BackColor = Color.Transparent Then
            Me.BackColor = Color.White
        End If
    End Sub

    Private Sub myPictureBox_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Me.Focus()
    End Sub
End Class

Now in my form I have an openFileDialogue so I can pick a file. Then I want it to draw it in GDI+ on my form and enable it to be dragable.

I've tested the dragging before so I know it works I just don't know how to do this. This is the code I have so far:
Button7 is the button that creates my dragable image.
I want the user be able to create multiple images so he can have more then one thing in his form. Thats why I dimmed i as integer then used it as a number after pic which is myPictureBox class.

VB.NET:
Private currentX As Integer, currentY As Integer
Private isDragging As Boolean = False
 Dim pic(i) As myPictureBox
    Dim i As Integer = 0
    Dim newimage = pic(i)
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        i = i + 1
        Me.Controls.Add(newimage)
        AddHandler pic(i).MouseDown, AddressOf newimage_MouseDown
        AddHandler pic(i).MouseUp, AddressOf newimage_MouseUp
        AddHandler pic(i).MouseMove, AddressOf newimage_MouseMove
        Me.Refresh()
        pic(i).Image = New Bitmap(imagetxtbox.Text)
        pic(i).Width = pic(i).Image.Width + 2
        pic(i).Height = pic(i).Image.Height + 2
        With pic(i)
            .Top = 130 - pic(i).Height
            .Left = 350 - pic(i).Width
            .BackColor = Color.Transparent
            .SizeMode = PictureBoxSizeMode.CenterImage
            .Parent = Me
        End With
    End Sub

    Private Sub newimage_MouseDown(sender As Object, e As MouseEventArgs)
        isDragging = True
        currentX = e.X
        currentY = e.Y
    End Sub
    Private Sub newimage_MouseUp(sender As Object, e As MouseEventArgs)
        isDragging = False
    End Sub
    Private Sub newimage_MouseMove(sender As Object, e As MouseEventArgs)
        If isDragging = True Then
            pic(i).Top = pic(i).Top + (e.Y - currentY)
            pic(i).Left = pic(i).Left + (e.X - currentX)
        End If
    End Sub

Thanks in advance!
 
Back
Top