4 Replies - 395 Views - Last Post: 05 October 2012 - 07:04 AM Rate Topic: -----

#1 LacksdaSkillz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 12

Creating new dragable images

Posted 04 October 2012 - 07:12 AM

Hey all.

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

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.

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating new dragable images

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Creating new dragable images

Posted 04 October 2012 - 07:45 AM

For this you need a simple class to hold a few properties so you have an object that will persist. Then apply the same mouse events for the canvas you are drawing on.

Public Class DragableImage
  Public Property Rect As Rectangle
  Public Property Img As Image
End Class

Was This Post Helpful? 0
  • +
  • -

#3 LacksdaSkillz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 12

Re: Creating new dragable images

Posted 04 October 2012 - 07:52 AM

Good Idea! I will begin with that and tell you how it goes.
Was This Post Helpful? 0
  • +
  • -

#4 LacksdaSkillz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-September 12

Re: Creating new dragable images

Posted 05 October 2012 - 01:30 AM

View Post_HAWK_, on 04 October 2012 - 07:45 AM, said:

For this you need a simple class to hold a few properties so you have an object that will persist. Then apply the same mouse events for the canvas you are drawing on.

Public Class DragableImage
  Public Property Rect As Rectangle
  Public Property Img As Image
End Class



OK. I understand where your getting at but I am stuck on how to use it. Do you have a Skype we could talk there a bit more and you could help me when I am stuck a long the way. I need a good mentor :)
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Creating new dragable images

Posted 05 October 2012 - 07:04 AM

You create a new object(DragableImage), set it's image, move it with similar code as you have in your mouse events. Explore the rectangle class and you will see some familiar properties. Since you are handling the painting of your objects you need a canvas - Picturebox works great and use it's paint event. In the paint event you use the e.Graphics object for drawing. You need to explore this area much further. Check out Bob Powell's site - my mentor for graphics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1