Public Class Frame ' The class sets an image and resizes the 'picturebox size matching 'the image size 'It also moves itself onto the screen when the 'mousedown and mousemove event are raised Inherits PictureBox Private lastmouselocation As New Point Public Property myimage() As Image Get Return Me.Image End Get Set(ByVal value As Image) Me.Image = value Me.Size = value.Size End Set End Property Private Sub DrawFrame(ByVal gr As System.Drawing.Graphics) gr.DrawRectangle(New Pen(Color.Blue, 3), Me.ClientRectangle) End Sub Private Sub Frame_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown lastmouselocation = New Point(e.Location) End Sub Private Sub Frame_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then Me.Left += e.Location.X - lastmouselocation.X Me.Top += e.Location.Y - lastmouselocation.Y End If End Sub Private Sub Frame_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint DrawFrame(e.Graphics) End Sub End Class
Public Class Form1 Private myframe As New Frame Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load myframe.myimage = Image.FromFile(Application.StartupPath & "\im1.jpg") myframe.Location = New Point(10, 10) Me.Controls.Add(myframe) End Sub End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Class Frame ' The class sets an image and resizes the 'picturebox size matching 'the image size 'It also moves itself onto the screen when the 'mousedown and mousemove event are raised Inherits PictureBox Private lastmouselocation As New Point Public Property myimage() As Image Get Return Me.Image End Get Set(ByVal value As Image) Me.Image = value Me.Size = value.Size End Set End Property Private Sub Frame_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown lastmouselocation = New Point(e.Location) End Sub Private Sub Frame_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then Me.Left += e.Location.X - lastmouselocation.X Me.Top += e.Location.Y - lastmouselocation.Y End If End Sub End Class
Public Class Form1 Private myframe As New Frame Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load myframe.myimage = Image.FromFile(Application.StartupPath & "\happy3.JPG") myframe.Location = New Point(10, 10) Me.Controls.Add(myframe) End Sub Private Sub DrawFrame(ByVal gr As System.Drawing.Graphics) gr.DrawRectangle(New Pen(Color.Blue, 3), myframe.Left - 4, myframe.Top - 4, myframe.Width + 8, myframe.Height + 8) End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint DrawFrame(e.Graphics) End Sub End Class
Je voudrais dessiner un Rectangle, mettre une PictureBox a l'interieur et bouger les 2 ensemble.
Option Explicit On Public Class Form1 Private deplace As Nullable(Of Point) Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown deplace = PointToClient(Control.MousePosition) - PictureBox1.Location End Sub Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp deplace = Nothing End Sub Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove If deplace Is Nothing Then Exit Sub PictureBox1.Location = PointToClient(Control.MousePosition) - deplace.Value RectangleShape1.Left = PictureBox1.Left - 20 RectangleShape1.Top = PictureBox1.Top - 20 End Sub End Class
Public Class Frame Inherits PictureBox Private lastmouselocation As New Point Public Property myimage() As Image Get Return Me.Image End Get Set(ByVal value As Image) Me.SizeMode = PictureBoxSizeMode.CenterImage Me.Image = value Me.Width = value.Width + 30 Me.Height = value.Height + 30 End Set End Property Private Sub DrawFrame(ByVal gr As System.Drawing.Graphics) gr.DrawRectangle(New Pen(Color.Blue, 2), Me.Location.X, Me.Location.Y, Me.Width - 20, Me.Height - 20) End Sub Private Sub Frame_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown lastmouselocation = New Point(e.Location) End Sub Private Sub Frame_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then Me.Left += e.Location.X - lastmouselocation.X Me.Top += e.Location.Y - lastmouselocation.Y End If End Sub Private Sub Frame_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint DrawFrame(e.Graphics) End Sub End Class
Public Class Form1 Private myframe As New Frame Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load myframe.myimage = Image.FromFile(Application.StartupPath & "\happy3.JPG") myframe.Location = New Point(10, 10) Me.Controls.Add(myframe) End Sub End Class
A vrai dire, j'ai vraiment besoin du Rectangle, car je prevois d'utiliser certaines proprietes du Rectangle plus tard.
A vrai dire, j'ai vraiment besoin du Rectangle, car je prevois d'utiliser certaines proprietes du Rectangle plus tard.
Raison de plus alors
Public Class Form1 'déclaration d'un picturebox de fond rouge Dim WithEvents MonPictRectangle As New PictureBox With {.Parent Me, .BackColor Color.Red, .Size = New Size(200, 200)} 'déclaration d'un picturebox enfant de MonPictRectangle (de fond rouge ou avec une image) Dim WithEvents MonPictImage As New PictureBox With {.Parent MonPictRectangle, .BackColor Color.Blue, .Size = New Size(150, 150), .Location = New Point(25, 25)} 'déclaration d'un poijnt Dim MonPoint As Point Private Sub MonPictRectangle_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MonPictRectangle.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then 'mémorisation du point en vue du déplacement MonPoint = e.Location 'préparation du régionnage (centre du picturebox découpé) Dim MesPoints() As Point = {New Point(0, 0), _ New Point(200, 0), _ New Point(200, 200), _ New Point(0, 200), _ New Point(0, 0), _ New Point(25, 25), _ New Point(175, 25), _ New Point(175, 175), _ New Point(25, 175), _ New Point(25, 25)} Dim _types(MesPoints.Length - 1) As Byte For x = 0 To MesPoints.Length - 1 _types(x) = Ctype(Drawing2D.PathPointType.Line,byte) Next Dim gp As New Drawing2D.GraphicsPath(MesPoints, _types) 'régionnage MonPictRectangle.Region = New Region(gp) End If End Sub Private Sub MonPictRectangle_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MonPictRectangle.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then 'calcul de la position du picturebox pendnant le déplacement MonPictRectangle.Location = Point.Subtract(Cursor.Position, _ New Size(Me.Left + MonPoint.X, _ Me.Top + MonPoint.Y + SystemInformation.CaptionHeight)) End If End Sub Private Sub MonPictRectangle_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MonPictRectangle.MouseUp If e.Button = Windows.Forms.MouseButtons.Left Then 'annulation du régionnage MonPictRectangle.Region = Nothing End If End Sub End Class
Ni la bordure d'un contenant, ni sa couleur de fond, ni la "fausse bordure" résultant d'un positionnement de l'image au sein du contenant ne sauraient par exemple transformer à la demande et selon les circonstances (signaler par exemple son "accrochage" en vue de redimensionnement ou autre) l'aspect du périmètre du rectangle (pointillés, tirets, etc...).