0

I'm trying to get a picture or more specifically an animation to follow the mouse across the whole computer, not just inside of a form. I found this little snippet of code

Public Class Form1

Private Sub Form1_MouseMove(ByVal sender As Object, _
                            ByVal e As System.Windows.Forms.MouseEventArgs) _
                            Handles Me.MouseMove
    PictureBox1.Top = MousePosition.Y - Me.Top
    PictureBox1.Left = MousePosition.X - Me.Left
End Sub

End Class

and that works dandy so now I'd like to do this without the form.

Any help would be greatly appreciated.

1 Answers1

0

You can't really ditch the form but you can work your way around it. Create form, set Borderstyle to none and TopMost to true. Add a Picturebox and set the .Dock property of the picturebox to All.
The timer in my code is used because we will make the form click-trough and then the Mouseevents will not work properly.

Use the following code to make the form follow the mousecursor.

Public Class Form1
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="GetWindowLong")> Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SetWindowLong")> Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function

Const xOff As Integer = 0 'How far the form will be away from the curson in x-direction
Const yOff As Integer = 0 'How far the form will be away from the curson in y-direction

Private WithEvents Timer1 As Timer

Private Sub Timer1_Tick(sender As Object, e As EventArgs)
    Me.SetDesktopLocation(MousePosition.X - xOff, MousePosition.Y - yOff) 'Set the form's position
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Setup a timer
    Timer1 = New Timer
    Timer1.Interval = 1
    AddHandler Timer1.Tick, AddressOf Timer1_Tick
    Timer1.Start()

    'Set the form to clickthrough
    'See http://stackoverflow.com/questions/18294658/vb-net-click-through-form
    Dim InitialStyle As Integer
    InitialStyle = GetWindowLong(Me.Handle, -20)
    SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20) 'Makes the window "click-throughable"
End Sub
End Class

It has problems with other TopMost-windows and switching programs through the taskbar but it's a start.

Jens
  • 6,275
  • 2
  • 25
  • 51