Public Class Form1
Private handleSize As Integer = 5
Private overHandle As Boolean
Private inPB As Boolean = False
Private resizing As Boolean = False
Private rc1 As Rectangle
Private rc2 As Rectangle
Private rc3 As Rectangle
Private rc4 As Rectangle
Private startPoint As Point
Private endPoint As Point
Private lastPoint As Point
Private screenRC As Rectangle
Private PanelTop As Integer
Private PanelLeft As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
UpdateHandles()
inPB = True
PictureBox1.Refresh()
End Sub
Private Sub UpdateHandles()
rc1 = New Rectangle(PanelLeft, PanelTop, handleSize, handleSize)
rc2 = New Rectangle(PanelLeft, PanelTop + PictureBox1.Height - (handleSize + 2), handleSize, handleSize)
rc3 = New Rectangle(PanelLeft + PictureBox1.Width - (handleSize + 2), PanelTop, handleSize, handleSize)
rc4 = New Rectangle(PanelLeft + PictureBox1.Width - (handleSize + 2), PanelTop + PictureBox1.Height - (handleSize + 2), handleSize, handleSize)
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If inPB Then
e.Graphics.FillRectangle(Brushes.Red, rc1)
e.Graphics.FillRectangle(Brushes.Red, rc2)
e.Graphics.FillRectangle(Brushes.Red, rc3)
e.Graphics.FillRectangle(Brushes.Red, rc4)
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left AndAlso overHandle Then
resizing = True
If rc1.Contains(e.X, e.Y) Then
startPoint = New Point(PanelLeft + PictureBox1.Width, PanelTop + PictureBox1.Height)
endPoint = New Point(PanelLeft, PanelTop)
ElseIf rc2.Contains(e.X, e.Y) Then
startPoint = New Point(PanelLeft + PictureBox1.Width, PanelTop)
endPoint = New Point(PanelLeft, PanelTop + PictureBox1.Height)
ElseIf rc3.Contains(e.X, e.Y) Then
startPoint = New Point(PanelLeft, PanelTop + PictureBox1.Height)
endPoint = New Point(PanelLeft + PictureBox1.Width, PanelTop)
ElseIf rc4.Contains(e.X, e.Y) Then
startPoint = New Point(PanelLeft, PanelTop)
endPoint = New Point(PanelLeft + PictureBox1.Width, PanelTop + PictureBox1.Height)
End If
lastPoint = New Point(e.X, e.Y)
screenRC = PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint))
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
overHandle = rc1.Contains(e.X, e.Y) OrElse rc2.Contains(e.X, e.Y) OrElse rc3.Contains(e.X, e.Y) OrElse rc4.Contains(e.X, e.Y)
If resizing Then
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
endPoint.X = endPoint.X + (e.X - lastPoint.X)
endPoint.Y = endPoint.Y + (e.Y - lastPoint.Y)
lastPoint = New Point(e.X, e.Y)
screenRC = PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint))
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If resizing Then
resizing = False
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
Dim newPosition As Rectangle = Panel1.RectangleToClient(PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint)))
PictureBox1.SuspendLayout()
PictureBox1.Location = newPosition.Location
PictureBox1.Size = newPosition.Size
PictureBox1.ResumeLayout()
UpdateHandles()
PictureBox1.Refresh()
End If
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
inPB = False
PictureBox1.Refresh()
End Sub
Private Function NormalizedRC(ByVal ptA As Point, ByVal ptB As Point) As Rectangle
Return New Rectangle(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y), Math.Abs(ptA.X - ptB.X), Math.Abs(ptA.Y - ptB.Y))
End Function
End Class
Dim newPosition As Rectangle = Me.RectangleToClient(PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint)))
Dim newPosition As Rectangle = Panel1.RectangleToClient(PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint)))
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionApparemment les deux solution fonctionnent
Public Class Form4
Private handleSize As Integer = 5
Private overHandle As Boolean
Private inPB As Boolean = False
Private resizing As Boolean = False
Private rc1 As Rectangle
Private rc2 As Rectangle
Private rc3 As Rectangle
Private rc4 As Rectangle
Private startPoint As Point
Private endPoint As Point
Private lastPoint As Point
Private screenRC As Rectangle
Dim Gauche As Integer
Dim Haut As Integer
Dim no As Boolean = False
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
UpdateHandles()
inPB = True
PictureBox1.Refresh()
End Sub
Private Sub UpdateHandles()
rc1 = New Rectangle(0, 0, handleSize, handleSize)
rc2 = New Rectangle(0, PictureBox1.Height - handleSize - 2, handleSize, handleSize)
rc3 = New Rectangle(PictureBox1.Width - handleSize - 2, 0, handleSize, handleSize)
rc4 = New Rectangle(PictureBox1.Width - handleSize - 2, PictureBox1.Height - handleSize - 2, handleSize, handleSize)
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If inPB Then
e.Graphics.FillRectangle(Brushes.Red, rc1)
e.Graphics.FillRectangle(Brushes.Red, rc2)
e.Graphics.FillRectangle(Brushes.Red, rc3)
e.Graphics.FillRectangle(Brushes.Red, rc4)
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Try
Gauche = e.X
Haut = e.Y
If e.Button = Windows.Forms.MouseButtons.Right Then
PictureBox1.Cursor = Cursors.SizeAll
End If
Catch ex As Exception
Exit Sub
End Try
If e.Button = Windows.Forms.MouseButtons.Left AndAlso overHandle Then
resizing = True
If rc1.Contains(e.X, e.Y) Then
startPoint = New Point(PictureBox1.Width, PictureBox1.Height)
endPoint = New Point(0, 0)
ElseIf rc2.Contains(e.X, e.Y) Then
startPoint = New Point(PictureBox1.Width, 0)
endPoint = New Point(0, PictureBox1.Height)
ElseIf rc3.Contains(e.X, e.Y) Then
startPoint = New Point(0, PictureBox1.Height)
endPoint = New Point(PictureBox1.Width, 0)
ElseIf rc4.Contains(e.X, e.Y) Then
startPoint = New Point(0, 0)
endPoint = New Point(PictureBox1.Width, PictureBox1.Height)
End If
lastPoint = New Point(e.X, e.Y)
screenRC = PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint))
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
overHandle = rc1.Contains(e.X, e.Y) OrElse rc2.Contains(e.X, e.Y) OrElse rc3.Contains(e.X, e.Y) OrElse rc4.Contains(e.X, e.Y)
If e.Button = Windows.Forms.MouseButtons.Right Then
PictureBox1.Location = New Point(PictureBox1.Left + e.X - Gauche, PictureBox1.Top + e.Y - Haut)
End If
If resizing Then
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
endPoint.X = endPoint.X + (e.X - lastPoint.X)
endPoint.Y = endPoint.Y + (e.Y - lastPoint.Y)
lastPoint = New Point(e.X, e.Y)
screenRC = PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint))
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If Not no = False Then
Windows.Forms.Cursor.Clip = Nothing
no = False
End If
If resizing Then
resizing = False
ControlPaint.DrawReversibleFrame(screenRC, PictureBox1.BackColor, FrameStyle.Dashed)
Dim newPosition As Rectangle = Panel1.RectangleToClient(PictureBox1.RectangleToScreen(NormalizedRC(startPoint, endPoint)))
PictureBox1.SuspendLayout()
PictureBox1.Location = newPosition.Location
PictureBox1.Size = newPosition.Size
PictureBox1.ResumeLayout()
UpdateHandles()
PictureBox1.Refresh()
End If
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
inPB = False
PictureBox1.Refresh()
End Sub
Private Function NormalizedRC(ByVal ptA As Point, ByVal ptB As Point) As Rectangle
Return New Rectangle(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y), Math.Abs(ptA.X - ptB.X), Math.Abs(ptA.Y - ptB.Y))
End Function
End Class