como Mover un shape con el mouse
Estoy tratando de programar un juego y necesito mover un shape con el mouse.Que el puntero desapaezca y el shape actue como puntero.Tambien nececito Que No pueda salir del Form El puntero.
Muchas Gracias
Muchas Gracias
Te pongo un ejemplo:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Sub Form_Load()
Dim wRect As RECT
GetWindowRect Me.hwnd, wRect 'Captura el area de la ventana
ClipCursor wRect ',confina el cursor allí
ShowCursor False ' y lo oculta
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Shape1.Move X - (Shape1.Width / 2), Y - (Shape1.Height / 2)
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShowCursor True 'muestra el cursor
ClipCursor ByVal 0 'y lo libera
End Sub
Ten cuidado con las funciones que hay en ese código,
por ejemplo si se ejecuta 3 veces seguidas:
ShowCursor False
Tendrás que ejecutar otras 3 veces:
ShowCursor True
para que se vea de nuevo el ratón.
El riesgo de ClipCursor es que se puede quedar el
ratón atrapado aunque tu programa haya terminado.
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Sub Form_Load()
Dim wRect As RECT
GetWindowRect Me.hwnd, wRect 'Captura el area de la ventana
ClipCursor wRect ',confina el cursor allí
ShowCursor False ' y lo oculta
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Shape1.Move X - (Shape1.Width / 2), Y - (Shape1.Height / 2)
End Sub
Private Sub Form_Unload(Cancel As Integer)
ShowCursor True 'muestra el cursor
ClipCursor ByVal 0 'y lo libera
End Sub
Ten cuidado con las funciones que hay en ese código,
por ejemplo si se ejecuta 3 veces seguidas:
ShowCursor False
Tendrás que ejecutar otras 3 veces:
ShowCursor True
para que se vea de nuevo el ratón.
El riesgo de ClipCursor es que se puede quedar el
ratón atrapado aunque tu programa haya terminado.
