1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
Imports System.Drawing.Imaging
Public Class formDragBox
Dim bmp As Bitmap
Private picLocation As Point = Point.Empty
'variables pour gerer le rectangle
Private dragBox As Rectangle = Rectangle.Empty
Private rectPen As New Pen(Color.Yellow, 2)
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.ResizeRedraw = True
'---- PictureBox source
Me.AllowDrop = True
Me.PicSrc.Image = My.Resources.Dessin1
Me.PicSrc.SizeMode = PictureBoxSizeMode.Normal
Me.PicSrc.AllowDrop = True
'---- PictureBox destination
Me.PicDest.AllowDrop = True
Me.PicDest.Image = My.Resources.ZebrasThree
Me.PicDest.SizeMode = PictureBoxSizeMode.Normal
End Sub
Private Sub PictureBoxSrc_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PicSrc.MouseDown
Me.PicSrc.DoDragDrop(Me.PicSrc.Image, DragDropEffects.Copy)
End Sub
Private Sub PictureBoxDest_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PicDest.DragEnter
If Not (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
e.Effect = DragDropEffects.None
Cursor.Current = Cursors.Arrow
Else
e.Effect = DragDropEffects.Copy
Cursor.Current = Cursors.Cross
End If
End Sub
Private Sub PictureBoxDest_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PicDest.DragOver
picLocation = CType(sender, PictureBox).PointToClient(New Point(e.X, e.Y))
' ----- start move controlbox.
Me.dragBox.Location = picLocation
Me.dragBox.Width = PicSrc.Width
Me.dragBox.Height = PicSrc.Height
'---- vertu de refresh image: (efface et redessine le rectangle à la bouvelle location)
Me.PicDest.Refresh()
Me.PicDest.Update()
End Sub
Private Sub PictureBoxDest_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PicDest.DragDrop
picLocation = CType(sender, PictureBox).PointToClient(New Point(e.X, e.Y))
'met à jour rectangle Location.
Me.dragBox.Location = picLocation
'dragrop
If Me.PicDest.Image.GetBounds(GraphicsUnit.Display).Contains(picLocation) Then
bmp = e.Data.GetData(DataFormats.Bitmap)
'teste -avec- et -sans- cette ligne comment la taille de bmp est ajustee a celle de l'image festination
bmp.SetResolution(Me.PicDest.Image.HorizontalResolution, Me.PicDest.Image.VerticalResolution)
If (bmp IsNot Nothing) And _
Not (Me.picLocation.Equals(Point.Empty)) Then
Dim gr As Graphics = Graphics.FromImage(Me.PicDest.Image)
gr.DrawImage(bmp, Me.picLocation)
'restore controlbox to empty rectangle
Me.dragBox = Rectangle.Empty
'---- vertu de refresh image: (efface et redessine cette un rectangle vide: neant)
Me.PicDest.Refresh()
Me.PicDest.Update()
End If
End If
End Sub
' Me.PicDest.Refresh() & Me.PicDest.Update() appelle cette sub
Private Sub PicDest_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PicDest.Paint
e.Graphics.DrawRectangle(Me.rectPen, dragBox)
End Sub
End Class |
Partager