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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
'ton form "parent"
Public Class frmAddPic
'le picture est cree dans FrmPageBis
Private newFrm As FrmPage
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
'form de plus haut niveau
Me.TopLevel = TopLevel
End Sub
Private Sub btnCreateFrmPageBis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateFrmPageBis.Click
newFrm = New FrmPage()
'frmAddPic est parent de FrmPage
newFrm.Owner = Me
newFrm.Show()
'desactive
Me.btnCreateFrmPageBis.Enabled = False
End Sub
'compteure pour ajouter des picturebox
Private Shared counterPic As Integer = 0
Private Sub BtnAddNewPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddNewPicture.Click
If newFrm Is Nothing Then
Return
End If
Dim rnd As Random = New Random
counterPic = counterPic + 1
Dim clr As Color
Dim strNomPic As String = String.Empty
strNomPic = "myPic" & counterPic
clr = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))
newFrm.AddPic(strNomPic, clr)
End Sub
End Class
'le form "fils"
Public Class FrmPage
'MouseOffset represente position souris
'-mise constamment à jour- lorsque LeftButton est appuye.
Private mouseOffset As Point
Private isMouseDown As Boolean = False
'une variable picbox
Private currentPB As PictureBox
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
'
'tu peux activer le scroll si jamais le picbox est deplace
'hors fenetre
'
'Me.AutoScroll = True
End Sub
Public Sub AddPic(ByVal namePic As String, ByVal bckcolor As Color)
currentPB = New PictureBox
With currentPB
.Name = namePic
.BorderStyle = BorderStyle.Fixed3D
.Size = New Size(200, 200)
.Location = New Point(50, 150)
.BackColor = bckcolor
End With
'ajoute les 3 handlers
AddHandler Me.currentPB.MouseDown, AddressOf commonPB_MouseDown
AddHandler Me.currentPB.MouseMove, AddressOf commonPB_Move
AddHandler Me.currentPB.MouseUp, AddressOf commonPB_MouseUp
Me.Controls.Add(currentPB)
End Sub
Private Sub commonPB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim xOffset As Integer
Dim yOffset As Integer
If e.Button = MouseButtons.Left Then
xOffset = e.X
yOffset = e.Y
mouseOffset = Me.PointToClient(New Point(xOffset, yOffset))
isMouseDown = True
End If
End Sub
'dans MouseMove il faut utiliser des coord. ecrans
'car il n' y a pas d'autre choix
Private Sub commonPB_Move(ByVal sender As Object, ByVal e As System.EventArgs)
If isMouseDown Then
'convertir en coords client fenetre frmPage
Dim mousePos As Point = Me.PointToClient(Control.MousePosition)
'mettre à jour position du picturebox
Me.currentPB.Location = mousePos
Me.Refresh()
End If
End Sub
Private Sub commonPB_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
' Changes the isMouseDown field so that the picturebox does
' not move unless the user is pressing the left mouse button.
If e.Button = MouseButtons.Left Then
isMouseDown = False
End If
End Sub
End Class |
Partager