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
|
Public Class Form1
Inherits System.Windows.Forms.Form
Dim WithEvents Picture As PictureBox
Dim R As Rectangle
Dim allowMove As Boolean = False
Private oldpos As New System.Drawing.Point(0, 0)
Private oldPosRec As New System.Drawing.Point(0, 0)
#Region " Code généré par le Concepteur Windows Form "
Public Sub New()
MyBase.New()
'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
'Ajoutez une initialisation quelconque après l'appel InitializeComponent()
End Sub
'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer
'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form
'Elle peut être modifiée en utilisant le Concepteur Windows Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(800, 637)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Picture = New PictureBox
Dim image As New Bitmap("c:\test\1.jpg")
Picture.Image = image
Picture.Size = image.Size
Picture.Location = New System.Drawing.Point((Me.Width - Picture.Width) / 2, (Me.Height - Picture.Height) / 2)
Me.Controls.Add(Picture)
End Sub
Private Sub Picture_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Picture.Paint
Dim blackPen As New Pen(Color.Black, 2)
R = New Rectangle(R.X, R.Y, 100, 100)
e.Graphics.DrawRectangle(blackPen, R)
End Sub
Private Sub Picture_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture.MouseDown
If e.X > R.X And e.X < R.X + R.Width Then
If e.Y > R.Y And e.Y < R.Y + R.Height Then
Picture.Cursor = Cursors.Hand
oldpos.X = e.X
oldpos.Y = e.Y
oldPosRec.X = R.X
oldPosRec.Y = R.Y
allowMove = True
End If
End If
End Sub
Private Sub Picture_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture.MouseMove
If allowMove Then
R.X = e.X - (oldpos.X - oldPosRec.X)
R.Y = e.Y - (oldpos.Y - oldPosRec.Y)
If R.X > Picture.Width - R.Width Then
R.X = Picture.Width - R.Width
End If
If R.X < 0 Then
R.X = 0
End If
If R.Y > Picture.Height - R.Height Then
R.Y = Picture.Height - R.Height
End If
If R.Y < 0 Then
R.Y = 0
End If
Picture.Invalidate()
End If
End Sub
Private Sub Picture_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture.MouseUp
allowMove = False
Picture.Cursor = Cursors.Default
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Not Picture Is Nothing Then
Picture.Location = New System.Drawing.Point((Me.Width-Picture.Width) / 2,(Me.Height - Picture.Height) / 2)
End If
End Sub
End Class |
Partager