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 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
Public Class Form2
Private calque As Bitmap = Nothing
Private dropppedPictures As New List(Of DroppedPic)
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
PicModel.AllowDrop = True
End Sub
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DrawImages(PicModel, Color.Yellow)
calque = New Bitmap(PicModel.Width, PicModel.Height)
DrawImages(Pic1, Color.Red)
DrawImages(Pic2, Color.Blue)
DrawImages(Pic3, Color.Magenta)
End Sub
Private Sub DrawImages(pic As PictureBox, clr As Color)
Dim imgPic As New Bitmap(pic.Width, pic.Height)
Using grfx As Graphics = Graphics.FromImage(imgPic)
grfx.Clear(clr)
End Using
pic.Image = imgPic
End Sub
Private m_MouseIsDown As Boolean
Private m_DroppedPic As DroppedPic = Nothing
Private Sub Pic3_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Pic3.MouseDown, Pic2.MouseDown, Pic1.MouseDown
'pic sender
Dim picSender As PictureBox = CType(sender, PictureBox)
If Not picSender.Image Is Nothing Then
' Set a flag to show that the mouse is down.
m_MouseIsDown = True
m_DroppedPic = New DroppedPic() With {.Picture = picSender}
End If
End Sub
Private Sub Pic3_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Pic3.MouseMove, Pic2.MouseMove, Pic1.MouseMove
'pic sender
Dim picSender As PictureBox = CType(sender, PictureBox)
If m_MouseIsDown Then
' Initiate dragging and allow either copy or move.
picSender.DoDragDrop(picSender.Image, DragDropEffects.Copy)
End If
m_MouseIsDown = False
End Sub
Private Sub PicModel_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles PicModel.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub PicModel_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles PicModel.DragDrop
'ici mode habituel du drag drop
'alas ,il a pour egget de remplacer l'image "model" par l'image "degatus catastropgus"
'Assign the image to the PictureBox.
'PicModel.Image = e.Data.GetData(DataFormats.Bitmap)
Dim degatImg As Bitmap = e.Data.GetData(DataFormats.Bitmap)
If degatImg IsNot Nothing Then
Dim pos As Point = PicModel.PointToClient(New Point(e.X, e.Y)) 'convertit les coords relatives à l'ecran(Screen) en coords "client" du PicModel
m_DroppedPic.AtPoint = pos
dropppedPictures.Add(m_DroppedPic)
'Draw le calque de Preview sur la surface du PictModel(pic.image n'est pas altéré)
PicModel.Invalidate()
End If
End Sub
Private Sub PicModel_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PicModel.Paint
Dim gr As Graphics = e.Graphics
'reset calque
calque = New Bitmap(PicModel.Width, PicModel.Height) 'EN GDI CA CREE UN BITMAP TRANSPARENT
Using grfx As Graphics = Graphics.FromImage(calque)
For Each item In dropppedPictures
grfx.DrawImage(item.Picture.Image, item.AtPoint)
Next
End Using
gr.DrawImage(calque, 0, 0)
End Sub
Private Sub btnResetCalque_Click(sender As System.Object, e As System.EventArgs) Handles btnResetCalque.Click
If calque IsNot Nothing Then calque = New Bitmap(calque.Width, calque.Height) 'min lahaytou bakherlou
'efface la liste
dropppedPictures.Clear()
PicModel.Invalidate()
End Sub
Private Sub Pic3_Click(sender As System.Object, e As System.EventArgs) Handles Pic3.Click, Pic2.Click, Pic1.Click
Dim clicked As PictureBox = CType(sender, PictureBox)
Dim target As DroppedPic = dropppedPictures.Find(Function(x) x.Picture Is clicked)
If target IsNot Nothing Then
dropppedPictures.Remove(target) 'supprime le DroppedPic
PicModel.Invalidate()
End If
End Sub
End Class
Public Class DroppedPic
Public Property Picture As PictureBox
Public Property AtPoint As Point
End Class |
Partager