Imports System.Runtime.InteropServices Public Class Form1 Private lastMouseLocation As New Point Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim pctDPicture As New PictureBox pctDPicture.SizeMode = PictureBoxSizeMode.StretchImage pctDPicture.BackColor = Color.Black pctDPicture.Size = New Size(150, 100) pctDPicture.Location = New Point(20, 20) AddHandler pctDPicture.MouseMove, AddressOf DPictureMouseMove AddHandler pctDPicture.MouseDown, AddressOf DPictureMouseDown AddHandler pctDPicture.MouseClick, AddressOf DPictureMouseClick Me.Controls.Add(pctDPicture) pctDPicture.BringToFront() End Sub Private Sub DPictureMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) ' Movement of a dynamic PictureBox Dim pt As New Point pt = e.Location If e.Button = Windows.Forms.MouseButtons.Left Then pt.X = (pt.X + DirectCast(sender, PictureBox).Left) - lastMouseLocation.X pt.Y = (pt.Y + DirectCast(sender, PictureBox).Top) - lastMouseLocation.Y DirectCast(sender, PictureBox).Location = pt DirectCast(sender, PictureBox).BringToFront() End If End Sub Private Sub DPictureMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) ' Last Mouse location lastMouseLocation = e.Location End Sub 'Private Sub DPictureMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Private Sub DPictureMouseClick(ByVal sender As Object, ByVal e As System.EventArgs) Beep() ' Selection of a PictureBox by clicking on it DirectCast(sender, PictureBox).BorderStyle = BorderStyle.Fixed3D DirectCast(sender, PictureBox).BringToFront() End Sub End Class
ReleaseCapture() SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
Public Class Form1 Dim WithEvents pctBox As New PictureBox With {.BackColor Color.Red, .Parent Me, .Bounds = New Rectangle(10, 10, 200, 30)} Private Sub pctBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctBox.MouseClick Dim rect As Rectangle = pctBox.Bounds pctBox.Bounds = Rectangle.Inflate(rect, 3, 3) pctBox.Location = New Point(rect.Left, rect.Top) End Sub End Class
Public Class Form1 Dim WithEvents pctBox As New PictureBox With {.BackColor Color.Red, .Parent Me, .Bounds = New Rectangle(10, 10, 200, 30)} Dim pt As Point Private Sub pctBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctBox.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then 'agrandir Dim rect As Rectangle = pctBox.Bounds pctBox.Bounds = Rectangle.Inflate(rect, 3, 3) pctBox.Location = New Point(rect.Left, rect.Top) ElseIf Windows.Forms.MouseButtons.Left Then 'déplacement pt = e.Location End If End Sub Private Sub pctBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctBox.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then 'déplacement pctBox.Location = Point.Subtract(Cursor.Position, _ New Size(Me.Left + pt.X, _ Me.Top + pt.Y + SystemInformation.CaptionHeight)) End If End Sub End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question