Jump to content

VB2010 - movimento


falcan
 Share

Recommended Posts

Encontrei este código na internet .... esta aqui se alguém quiser fazer alguma coisa se mexer ....

Para os botões Numericos

basta criar uma ovalshape e depois no Form_KeyDown inserir este código .

If e.KeyCode = Keys.NumPad2 Then
            Dim i As String = OvalShape1.Location.X, s As String = OvalShape1.Location.Y + 10
            OvalShape1.Location = New Point(i, s)
        ElseIf e.KeyCode = Keys.NumPad8 Then
            Dim b As String = OvalShape1.Location.X
            Dim g As String = OvalShape1.Location.Y - 10
            OvalShape1.Location = New Point(b, g)
        ElseIf e.KeyCode = Keys.NumPad4 Then
            OvalShape1.Left -= 10
        ElseIf e.KeyCode = Keys.NumPad6 Then
            OvalShape1.Left += 10
        End If

para As Setas de direcção

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean


        If keyData = Keys.Down Then
            Dim i As String = OvalShape1.Location.X, s As String = OvalShape1.Location.Y + 10
            OvalShape1.Location = New Point(i, s)
        ElseIf keyData = Keys.Up Then
            Dim b As String = OvalShape1.Location.X
            Dim g As String = OvalShape1.Location.Y - 10
            OvalShape1.Location = New Point(b, g)
        ElseIf keyData = Keys.Left Then
            OvalShape1.Left -= 10
        ElseIf keyData = Keys.Right Then
            OvalShape1.Left += 10
        End If
Return MyBase.ProcessCmdKey(msg, keyData)


End Function[/code = vbnet]


Um obrigado especial para jpaulino pela ajuda do movimento das setas de direcção 






Link to comment
Share on other sites

Não andes a apagar mensagens 😉

Faz assim:

     Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean


        If keyData = Keys.Up Then
            ' ...
            Stop
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function
Link to comment
Share on other sites

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Down Then


            Dim i As String = OvalShape1.Location.X, s As String = OvalShape1.Location.Y + 10

            OvalShape1.Location = New Point(i, s)

        ElseIf e.KeyCode = Keys.Up Then

            Dim b As String = OvalShape1.Location.X
            Dim g As String = OvalShape1.Location.Y - 10

            OvalShape1.Location = New Point(b, g)

        ElseIf e.KeyCode = Keys.Left Then

            OvalShape1.Left -= 10

        ElseIf e.KeyCode = Keys.Right Then

            OvalShape1.Left += 10

        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub
End Class

tenho isto assim ...

e agora onde insiro o novo código ?

Link to comment
Share on other sites

Em vez disso, usas o Overrides à função ProcessCmdKey que te mostrei:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

            If keyData  = Keys.Down Then

                Dim i As String = OvalShape1.Location.X, s As String = OvalShape1.Location.Y + 10

                OvalShape1.Location = New Point(i, s)

          elseif

                ' ...

        Return MyBase.ProcessCmdKey(msg, keyData)

    End Function
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.