Usando la api de windows
Código fuenteOption Explicit On
Option Strict On
Public Class Form1
' Apis
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Integer,ByVal wMsg As Integer,ByVal wParam As Integer, _
ByRef lParam As Object) As Integer
Private Declare Function ReleaseCapture Lib "user32" () As Integer
' constantes / variables
Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Private Const HTCAPTION As Integer = 2
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.TopMost = True
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
' pasar el formulario como parámetro
Mover_Formulario(Me)
End If
End Sub
Sub Mover_Formulario(ByVal frm As Form)
ReleaseCapture()
'cambiar el puntero
Me.Cursor = Cursors.NoMove2D
Dim ret As Integer = SendMessage(frm.Handle.ToInt32, _
WM_NCLBUTTONDOWN,HTCAPTION, 0)
' reestablecer el cursor al soltar
Me.Cursor = Cursors.Default
End Sub
End Class
Option Strict On
Public Class Form1
' Apis
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Integer,ByVal wMsg As Integer,ByVal wParam As Integer, _
ByRef lParam As Object) As Integer
Private Declare Function ReleaseCapture Lib "user32" () As Integer
' constantes / variables
Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Private Const HTCAPTION As Integer = 2
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.TopMost = True
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
' pasar el formulario como parámetro
Mover_Formulario(Me)
End If
End Sub
Sub Mover_Formulario(ByVal frm As Form)
ReleaseCapture()
'cambiar el puntero
Me.Cursor = Cursors.NoMove2D
Dim ret As Integer = SendMessage(frm.Handle.ToInt32, _
WM_NCLBUTTONDOWN,HTCAPTION, 0)
' reestablecer el cursor al soltar
Me.Cursor = Cursors.Default
End Sub
End Class
Cambiando la coordenada x e y en el evento MouseMove del formulario
Option Explicit On
Option Strict On
Public Class Form1
' variables
Private x As Integer
Private y As Integer
Private mover As Boolean
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
' habilitar el flag
mover = True
' guardar las coordenadas
x = e.X
y = e.Y
' cambiar el cursor del mouse
Me.Cursor = Cursors.NoMove2D
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If mover Then
' establecer la nueva posición
Me.Location = New Point((Me.Left + e.X - x), (Me.Top + e.Y - y))
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
' reestablecer
mover = False
Me.Cursor = Cursors.Default
End Sub
End Class
Option Strict On
Public Class Form1
' variables
Private x As Integer
Private y As Integer
Private mover As Boolean
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
' habilitar el flag
mover = True
' guardar las coordenadas
x = e.X
y = e.Y
' cambiar el cursor del mouse
Me.Cursor = Cursors.NoMove2D
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If mover Then
' establecer la nueva posición
Me.Location = New Point((Me.Left + e.X - x), (Me.Top + e.Y - y))
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
' reestablecer
mover = False
Me.Cursor = Cursors.Default
End Sub
End Class
Mover un control con el mouse
Este ejemplo , al hacer clic en un control , se cambia el valor de x e y del objetoIniciar un nuevo proyecto, añadir un windows form, y colocar un control PictureBox
Código fuente
Option Explicit On
Option Strict On
Public Class Form1
Private Mover As Boolean = False
Private p_Mouse As Point = Nothing
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
Me.Cursor = Cursors.Hand
Mover = True
' guarda rl el x e y donde se hizo clic
p_Mouse.X = e.X
p_Mouse.Y = e.Y
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseMove
If Mover Then
' referencia al control
Dim unControl As Control = CType(sender, Control)
' cambiar las coordenadas
Dim p1 As Point = unControl.PointToScreen(e.Location)
Dim p2 As Point = unControl.Parent.PointToClient(p1)
' asignar el left y el top - laposición del mouse donde se hizo clic
unControl.Left = p2.X - p_Mouse.X
unControl.Top = p2.Y - p_Mouse.Y
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
' cambiar el cursors
Me.Cursor = Cursors.Default
' flag para mover el control
Mover = False
End Sub
End Class
Option Strict On
Public Class Form1
Private Mover As Boolean = False
Private p_Mouse As Point = Nothing
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
Me.Cursor = Cursors.Hand
Mover = True
' guarda rl el x e y donde se hizo clic
p_Mouse.X = e.X
p_Mouse.Y = e.Y
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseMove
If Mover Then
' referencia al control
Dim unControl As Control = CType(sender, Control)
' cambiar las coordenadas
Dim p1 As Point = unControl.PointToScreen(e.Location)
Dim p2 As Point = unControl.Parent.PointToClient(p1)
' asignar el left y el top - laposición del mouse donde se hizo clic
unControl.Left = p2.X - p_Mouse.X
unControl.Top = p2.Y - p_Mouse.Y
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
' cambiar el cursors
Me.Cursor = Cursors.Default
' flag para mover el control
Mover = False
End Sub
End Class
No hay comentarios:
Publicar un comentario