Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading the mouse movements to move a graphic???

Status
Not open for further replies.

Rmck

Programmer
Jul 14, 2003
3
US
I need to move a graphic object side to side based on the movement on the mouse. I'm working in VB.Net and i dont know how to read the movements of the mouse. I know I have to use the mousemove command, however I dont know how to use it correctly. As much detail and help would be greatly welcomed. Thanks alot!!!
-Ryan
 
Here is an example of moving a button. If your graphic object is not a control, it is more difficult to move it since it can't receive events and you have to erase and draw it every time. I think there is a way to draw non rectangular control, but I don't know how.
Code:
    Dim moving As Boolean = False
    Dim PrevX As Integer
    Dim PrevY As Integer

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

        moving = True
        Cursor = Cursors.Hand
        PrevX = MousePosition.X
        PrevY = MousePosition.Y

    End Sub

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

        moving = False
        Cursor = Cursors.Default

    End Sub

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

        If moving Then

            Button1.Location = New Point(Button1.Location.X + MousePosition.X - PrevX, Button1.Location.Y + MousePosition.Y - PrevY)
            PrevX = MousePosition.X
            PrevY = MousePosition.Y

        End If

    End Sub
 
I would kind of like it to be set so I dont have to use mouse down. If there is another way to do this without using the mouse click commands thats what I would like. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top