Exercício 1

Data de publicação: Mar 17, 2014 4:57:0 PM

MOUSE CLASS - ESTUDO DE CASO

//exemplo para mover 2 peças

Public Class Form1

Dim dragging1 As Boolean = False

Dim dragging2 As Boolean = False

Dim posicao1 As Point

Dim posicao2 As Point

Dim mouseDownX As Integer

Dim mouseDownY As Integer

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

dragging1 = True

posicao1 = PictureBox1.Location

mouseDownX = e.X + PictureBox1.Location.X

mouseDownY = e.Y + PictureBox1.Location.Y

End Sub

Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown

dragging2 = True

posicao2 = PictureBox2.Location

mouseDownX = e.X + PictureBox2.Location.X

mouseDownY = e.Y + PictureBox2.Location.Y

End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

If (dragging1) Then

Dim deltaX As Integer = e.X - mouseDownX

Dim deltaY As Integer = e.Y - mouseDownY

PictureBox1.Location = New Point(posicao1.X + deltaX, posicao1.Y + deltaY)

End If

If (dragging2) Then

Dim deltaX As Integer = e.X - mouseDownX

Dim deltaY As Integer = e.Y - mouseDownY

PictureBox2.Location = New Point(posicao2.X + deltaX, posicao2.Y + deltaY)

End If

End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

dragging1 = False

dragging2 = False

End Sub

Private Sub PictureBox1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Move

dragging1 = False

End Sub

Private Sub PictureBox2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.Move

dragging2 = False

End Sub

End Class