468,457 Members | 1,593 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

DragDrop PictureBox

akh
I want to use de Drag and Drop ´s event to move a picture box from a
form and within a Picture Box.

But I have behaviour if the MyPBox As PictureBox as the Globale
varible or not

Thanks for your help,

Here is the code.
Public Class Form1
Inherits System.Windows.Forms.Form
Private m_MouseIsDown As Boolean = False
Dim IdxButton As Integer
Friend WithEvents PictureBox1 As
System.Windows.Forms.PictureBox
Dim MyPBox As PictureBox
Dim MyIndex As Integer
Dim m_items As New ArrayList
Dim O_Sender As String
' True while we are drawing the new line in Picture4.
Private m_Drawing As Boolean
' The mouse position In Picture4.
Private m_X1 As Integer
Private m_Y1 As Integer
Private m_X2 As Integer
Private m_Y2 As Integer

' Buffer for erasing rubberband lines.
Private m_BufferBitmap As Bitmap
Private m_BufferGraphics As Graphics

'Public colPBox As New Collection 'Global variable

#Region " Code généré par le Concepteur Windows Form "
Public Sub New()
MyBase.New()
'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
End Sub
'La méthode substituée Dispose du formulaire pour nettoyer la
liste des composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer
'REMARQUE : la procédure suivante est requise par le
Concepteur Windows Form
'Elle peut être modifiée en utilisant le Concepteur Windows
Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
Friend WithEvents PictureTC As System.Windows.Forms.PictureBox
Friend WithEvents Picture4 As System.Windows.Forms.PictureBox
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.PictureTC = New System.Windows.Forms.PictureBox
Me.Picture4 = New System.Windows.Forms.PictureBox
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'PictureTC
'
Me.PictureTC.Image =
CType(resources.GetObject("PictureTC.Image"), System.Drawing.Image)
Me.PictureTC.Location = New System.Drawing.Point(24, 24)
Me.PictureTC.Name = "PictureTC"
Me.PictureTC.Size = New System.Drawing.Size(32, 32)
Me.PictureTC.TabIndex = 0
Me.PictureTC.TabStop = False
'
'Picture4
'
Me.Picture4.AccessibleRole =
System.Windows.Forms.AccessibleRole.Graphic
Me.Picture4.BackColor =
System.Drawing.SystemColors.Desktop
Me.Picture4.Location = New System.Drawing.Point(176, 16)
Me.Picture4.Name = "Picture4"
Me.Picture4.Size = New System.Drawing.Size(560, 368)
Me.Picture4.TabIndex = 1
Me.Picture4.TabStop = False
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.PictureTC)
Me.GroupBox1.Location = New System.Drawing.Point(24, 16)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(80, 352)
Me.GroupBox1.TabIndex = 2
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "GroupBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(760, 398)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.Picture4)
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub
#End Region

#Region "Form1"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
' Enable dropping.
Picture4.AllowDrop = True
End Sub
#End Region
#Region "gestion PictureTC"

Private Sub PictureTC_MouseDown(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
PictureTC.MouseDown
If Not PictureTC.Image Is Nothing Then
' Set a flag to show that the mouse is down.
m_MouseIsDown = True
End If
End Sub
Private Sub PictureTC_MouseMove(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.MouseEventArgs) Handles
PictureTC.MouseMove

If m_MouseIsDown Then
' Initiate dragging and allow either copy or move.
PictureTC.DoDragDrop(PictureTC.Image,
DragDropEffects.Copy)

End If
m_MouseIsDown = False
End Sub
Private Sub Picture4_DragEnter(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles Picture4.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
#End Region

#Region "gestion Picture4"

Private Sub Picture4_DragDrop(ByVal Source As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles Picture4.DragDrop
Dim Index As Integer
Dim Pbox As New PictureBox
Dim a
Dim n As Integer
Dim Sname As String
Dim toolTip1 As New ToolTip

If IsNewObj(O_Sender) Then
With Pbox
.Image = CType(e.Data.GetData(DataFormats.Bitmap),
Image)
.Height = 32
.Width = 32
.Visible = True
.AllowDrop = True
.Location = Me.Picture4.PointToClient(New
Point(e.X, e.Y))
.Name = "CmdObj" & Me.Picture4.Controls.Count
m_items.Add(Pbox)
Me.Picture4.Controls.Add(Pbox)
AddHandler Pbox.MouseDown, AddressOf
Pbox_MouseDown
End With
colPBox.Add(Pbox)
Else
Pbox.Name = O_Sender
Pbox.Location = Picture4.PointToClient(New Point(e.X,
e.Y))
O_Sender = Nothing
AddHandler Pbox.MouseMove, AddressOf Pbox_MouseMove
End If
' Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 1000
toolTip1.InitialDelay = 200
toolTip1.ReshowDelay = 500
' Force the ToolTip text to be displayed whether or not
the form is active.
toolTip1.ShowAlways = True
' Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(Pbox, Pbox.Name)
End Sub
Public Function IsNewObj(ByVal O_Sender As String) As Boolean
Dim TempBool As Boolean
If InStr(1, O_Sender, "CmdObj") Then 'Renvoie une valeur
de type Variant (Long) indiquant la position de la première occurrence
d'une chaîne à l'intérieur d'une autre chaîne.
TempBool = False
Else
TempBool = True
End If
IsNewObj = TempBool
End Function
Private Sub Pbox_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
sender.DoDragDrop(sender.Image, DragDropEffects.Move)
End If
m_MouseIsDown = True
End Sub
Private Sub Pbox_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
O_Sender = sender.name
If e.Button = MouseButtons.Left Then
sender.DoDragDrop(sender.Image, DragDropEffects.Copy)
End If
m_MouseIsDown = True
End Sub

#End Region


#Region "Gestion_ligne"
Private Overloads Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''############""

Dim myPen As New
System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
Dim PBox As PictureBox
Dim Obj1 As PictureBox
Dim Obj2 As PictureBox
Dim i As Integer

'formGraphics = Picture4.CreateGraphics()
Obj1 = colPBox.Item(1)
Obj2 = colPBox.Item(2)
DessineLigne(Obj1, Obj2)
'formGraphics.DrawLine(myPen, Obj1.Left, Obj1.Top + 16,
Obj2.Left, Obj2.Top + 16)
'formGraphics.Dispose()
'myPen.Dispose()
''#####################

End Sub
Public Sub DessineLigne(ByVal Obj1 As PictureBox, ByVal Obj2
As PictureBox)
Dim myPen As New
System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
Dim i As Integer
formGraphics = Picture4.CreateGraphics()
formGraphics.DrawLine(myPen, Obj1.Left, Obj1.Top + 16,
Obj2.Left, Obj2.Top + 16)
formGraphics.Dispose()
myPen.Dispose()
End Sub
Private Sub Picture4_MouseDown(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles Picture4.MouseDown

' Do nothing if this isn'tthe left mouse button.
If e.Button <> MouseButtons.Left Or colPBox.Count <= 1
Then Exit Sub
'If e.Button = MouseButtons.Right Then MsgBox("a")
m_Drawing = True

' Save a snapshot of the form.
SaveSnapshot()

' Save the current mouse position.
m_X1 = e.X
m_X2 = e.X
m_Y1 = e.Y
m_Y2 = e.Y
End Sub
Private Sub SaveSnapshot()
Dim new_bitmap As Bitmap

' Make a new bitmap that fits the form.
new_bitmap = New Bitmap(Me.Picture4.Size.Width,
Me.Picture4.Size.Height, Me.Picture4.CreateGraphics())
m_BufferGraphics = Graphics.FromImage(new_bitmap)

' Clear the new bitmap.
m_BufferGraphics.Clear(Me.Picture4.BackColor)

' Copy the existing bitmap's contents into
' the new bitmap.
If Not (m_BufferBitmap Is Nothing) Then
m_BufferGraphics.DrawImage(m_BufferBitmap, 0, 0)
End If

' Save the new bitmap and graphics objects.
m_BufferBitmap = new_bitmap
End Sub

Private Sub Picture4_MouseMove(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles Picture4.MouseMove
' Do nothing if we're not drawing.
If Not m_Drawing Then Exit Sub

' Save the new point.
m_X2 = e.X
m_Y2 = e.Y

' Erase the previous line.
DrawForm(Me.Picture4.CreateGraphics())

' Draw the new line directly on the form.
Me.Picture4.CreateGraphics().DrawLine(Pens.Yellow, m_X1,
m_Y1, m_X2, m_Y2)

End Sub
' Redraw the saved buffer.
Private Sub DrawForm(ByVal gr As Graphics)
If Not (m_BufferBitmap Is Nothing) Then
gr.DrawImage(m_BufferBitmap, 0, 0)
End Sub
Private Sub Picture4_MouseUp(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles Picture4.MouseUp
' Do nothing if we're not drawing.
If Not m_Drawing Then Exit Sub
m_Drawing = False

' Save the new point.
m_X2 = e.X
m_Y2 = e.Y

' Draw the new line permanently on the buffer.
m_BufferGraphics.DrawLine( _
Pens.Red, m_X1, m_Y1, m_X2, m_Y2)

' Redraw to show the new line.
DrawForm(Me.Picture4.CreateGraphics())
End Sub

Private Sub Picture4_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Picture4.Paint
DrawForm(e.Graphics)
End Sub
#End Region
End Class
Nov 20 '05 #1
0 3587

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Kate | last post: by
reply views Thread by Gene Hubert | last post: by
3 posts views Thread by Gary Dunne | last post: by
3 posts views Thread by Meelis | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.