نوشته شده توسط
pouyansaraf
با سلام
من برای حرکت دادن یک PictureBox از قطعه کد زیر که در رویداد
MouseMove آن قرار میدهم استفاده میکنم
Private oControlOffsetPoint As Point
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim sX As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 1, 1)).Left - oControlOffsetPoint.X
Dim sY As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 1, 1)).Top - oControlOffsetPoint.Y
If e.Button = Windows.Forms.MouseButtons.Left Then
sender.Location = New Point(sX, sY)
PictureBox1.Left = sX
PictureBox1.Top = sY
EndIf
End Sub
دو مشکل دارم
1- جایگاه نشانگر موس در هنگام حرکت رضایت بخش نیست (اگر مانند حرکت picturebox خود دات نت باشد عالی است)
2- اینکه اگر بخواهم در محدوده خاص یک PictureBox دیگر آنرا حرکت دهم چه باید بکنم
راستی PictureBox1.Capture = False چه کار میکند؟
از این کد استفاده کنید. البته کد خود شماست فقط یه سری تغییرات توی اون ایجاد کردم.
Private Laste As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Laste = e.Location
Dim _sender = DirectCast(sender, PictureBox)
If Not _sender.Cursor.Equals(Cursors.SizeAll) Then
_sender.Cursor = Cursors.SizeAll
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim sX As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 1, 1)).Left - Laste.X
Dim sY As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 1, 1)).Top - Laste.Y
Dim _sender = DirectCast(sender, PictureBox)
If e.Button = Windows.Forms.MouseButtons.Left Then
_sender.Location = New Point(sX, sY)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Dim _sender = DirectCast(sender, PictureBox)
If Not _sender.Cursor.Equals(Cursors.Default) Then
_sender.Cursor = Cursors.Default
End If
End Sub
ولی کد شما چند ایراد داره:
1. از متغیر oControlOffsetPoint که اصلا به اون مقدار ندادی استفاده کردی.
2.مختصات کنترل رو دوبار تغییر دادی. (اول توسط Location و بعد Left و Top)
مشکل دوم شما رو هم متوجه نشدم. اگه یه توضیحی بدی ممنون می شم.