form follows cursor?

ncc1701d

New member
Joined
Mar 28, 2005
Messages
4
Programming Experience
Beginner
using VB.NET, can anyone here make a form follow my mouse cursor around my screen as i move my mouse. I am not sure if I should use setcapture or what?thanks for any help. A working example would be great but any help is nice, :)
thanks
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Here's an example that makes the form follow the cursor when the left mousebutton is clicked:
VB.NET:
Private mouse_offset As Point
Private Sub my_MouseDown(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.MouseEventArgs) _
  Handles MyBase.MouseDown
    mouse_offset = New Point(-e.X, -e.Y)
End Sub

Private Sub my_MouseMove(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.MouseEventArgs) _
  Handles MyBase.MouseMove
    If e.Button = MouseButtons.Left Then
        Dim mousePos As Point = Me.MousePosition
        mousePos.Offset(mouse_offset.X, mouse_offset.Y)
        Me.Location = mousePos
    End If
End Sub
You can also do that by overRiding WndProc, but I can't seem to find the code right now.

By the way, here's a description of the SetCapture function in the user32.dll library:
SetCapture captures the mouse for the specified window. When a window captures the mouse, it receives all mouse input messages, including those which would otherwise be sent to other windows. Capturing the mouse input allows a window to keep track of all mouse actions via mouse messages, but no other windows will receive the mouse input. When the capture is no longer needed, the mouse capture should be ended by calling ReleaseCapture.
 
Top Bottom