none
How would I create a shaking effect of my cursor using vb.net? RRS feed

  • Question

  • I would like to create a shaking effect of my cursor, similar of what would be seen if you would shake your mouse, just like this gif:

    https://gyazo.com/a3951820701125ef6f6b1c5add50967e

    The shaking effect should start at the current cursor position on the screen, then gradually move in each direction as the shaking effect continues.

    If possible, I would like the cursor to stay in the same radius as it started in, like the above gif, the cursor tends to stay in the same area as it started in.

    I would prefer if the directions that were chosen would be randomized so the shaking effect does not look like a pattern.

    I think the randomized directions could be completed using the below code, i'm just not quite sure how to best implement it to do the above shaking effect.

    Static rand As System.Random = New System.Random()
    Dim RandomMouseCoordinate = rand.Next(Coordinate1, Coordinate2) 

    • Edited by bbbbbb32 Tuesday, May 30, 2017 2:32 PM
    Tuesday, May 30, 2017 2:17 PM

Answers

  •  Perhaps like this...

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")> Private Shared Function GetForegroundWindow() As IntPtr
        End Function
    
        Private R As New Random
        Private mp As Point = Point.Empty
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TrackBar_JitterSpeed.TickFrequency = 10
            TrackBar_JitterSpeed.Minimum = -450
            TrackBar_JitterSpeed.Maximum = 0
            TrackBar_JitterSpeed.Value = -250
            TrackBar_JitterSpace.TickFrequency = 5
            TrackBar_JitterSpace.Minimum = 5
            TrackBar_JitterSpace.Maximum = 50
            TrackBar_JitterSpace.Value = 25
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
            Timer1.Enabled = CheckBox1.Checked
        End Sub
    
        Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar_JitterSpeed.ValueChanged
            Timer1.Interval = 50 + Math.Abs(TrackBar_JitterSpeed.Value)
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If GetForegroundWindow() <> Me.Handle AndAlso MouseButtons = MouseButtons.Left Then
                If mp = Point.Empty Then mp = MousePosition
                Cursor.Position = New Point(mp.X + R.Next(-TrackBar_JitterSpace.Value, TrackBar_JitterSpace.Value + 1), mp.Y + R.Next(-TrackBar_JitterSpace.Value, TrackBar_JitterSpace.Value + 1))
            Else
                mp = Point.Empty
            End If
        End Sub
    End Class
    
     

     


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Wednesday, May 31, 2017 9:49 PM
    • Marked as answer by bbbbbb32 Wednesday, May 31, 2017 10:59 PM
    Wednesday, May 31, 2017 8:16 PM

All replies

  • You want to bully somebody?

    That is where this always is used for.


    Success
    Cor

    Tuesday, May 30, 2017 3:10 PM
  • You want to bully somebody?

    That is where this always is used for.


    Success
    Cor

    No, it's used for a game, the user will have an option to toggle it on or off.
    Tuesday, May 30, 2017 4:10 PM
  • You want to bully somebody?

    That is where this always is used for.


    Success
    Cor

    No, it's used for a game, the user will have an option to toggle it on or off.

    Joey,

    Do you know what the trouble is? If the code is set here, it is easy for somebody to copy that. Then it can simply be used in a simple way to bully what some find "Fun", (I don't) or to use it in ransomeware. 

    There are of course alternatives. If you create a new question here, for instance with the question. 

    Is there a possibility to shake the horizon of an image in my game. Then there are some here who can make the nicest solutions. (Not me, although I've seen a lot of them so maybe I succeed, but I leave it to the experts to show that here in the forums).

    :-)


    Success
    Cor


    Tuesday, May 30, 2017 4:20 PM
  • However, 

    Thinking this over, if you mean with your game a windows forms project where it is about the cursor which is showed inside the form, you can use this kind of code. (it needs an animated gif)

    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Static status As Boolean = False
            Static MySpecialCursor As Windows.Forms.Cursor
            If status = False Then
                MySpecialCursor = SpecialCursor(1)
                Me.Cursor = MySpecialCursor
            Else
                MySpecialCursor.Dispose()
                Me.Cursor = Cursors.Default
            End If
            status = Not status
        End Sub
        Public Function SpecialCursor(Optional perc As Integer = -1) As System.Windows.Forms.Cursor
            Using bm1 As New Bitmap(48, 48)
                Using bm2 As New Bitmap(48, 48)
                    bm1.SetResolution(96, 96)
                    bm2.SetResolution(96, 96)
                    Using gr1 As Graphics = Graphics.FromImage(bm1)
                        gr1.DrawImage(My.Resources.RollingGif, 0, 0)
                        Dim backColor As Color = bm1.GetPixel(1, 1)
                        bm1.MakeTransparent(backColor)
                        Using gr2 As Graphics = Graphics.FromImage(bm2)
                            gr2.CompositingMode = Drawing2D.CompositingMode.SourceOver
                            gr2.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                            gr2.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                            gr2.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
                            gr2.DrawImage(bm1, 0, 0)
                            Dim ax As Single = 0
                            If perc > -1 Then
                                ax = (gr2.MeasureString(perc.ToString & "%", New Font("Arial", 9)).Width / 2)
                                gr2.DrawString(perc.ToString & "%", New Font("Arial", 9, FontStyle.Regular), Brushes.Black, 24 - ax, 16)
                            End If
                            Try
                                Dim ptr As IntPtr = bm2.GetHicon()
                                Return New System.Windows.Forms.Cursor(ptr)
                            Catch ex As Exception
                                Return Cursors.WaitCursor
                            End Try
                        End Using
                    End Using
                End Using
            End Using
        End Function
    
        
    End Class
    


    Success
    Cor

    Tuesday, May 30, 2017 4:32 PM
  • You want to bully somebody?

    That is where this always is used for.


    Success
    Cor

    No, it's used for a game, the user will have an option to toggle it on or off.

    Joey,

    Do you know what the trouble is? If the code is set here, it is easy for somebody to copy that. Then it can simply be used in a simple way to bully what some find "Fun", (I don't) or to use it in ransomeware. 

    There are of course alternatives. If you create a new question here, for instance with the question. 

    Is there a possibility to shake the horizon of an image in my game. Then there are some here who can make the nicest solutions. (Not me, although I've seen a lot of them so maybe I succeed, but I leave it to the experts to show that here in the forums).

    :-)


    Success
    Cor


    I do not intend to use it to annoy/bully/use as ransomeware, but I do see where you're coming from.

    The reason I wanted it was for my tool, which is for a specific game that automates certain things for the user if they decide to use that certain feature.

    Similar to this tool:
    https://gyazo.com/cc59ae53852e03f349036a397f45f64d

    I would have a checkbox to turn on / off the option, while it's on it will only activate if the game in question is found as a running process, if it's found + the user is holding down Left Mouse Button then it will activate the shake, if the user stops holding Left Mouse Button then it will automatically stop the shaking effect.

    Tuesday, May 30, 2017 5:01 PM
  • Did you try the code I supplied, it changes your cursor inside your form.

    To try it you have to add an animated gif for instance a shocking flashlight to the resources. 


    Success
    Cor

    Tuesday, May 30, 2017 10:12 PM
  • Just create an animated cursor that moves rapidly in a long-ish sequence of random movements.  That way when it repeats on a loop it won't seem like a pattern.  Once you've created the cursor file you should be able to set it as a custom cursor for your form fairly easily.
    • Proposed as answer by Frank L. Smith Wednesday, May 31, 2017 9:49 PM
    Wednesday, May 31, 2017 6:37 PM
  •  Perhaps like this...

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll")> Private Shared Function GetForegroundWindow() As IntPtr
        End Function
    
        Private R As New Random
        Private mp As Point = Point.Empty
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TrackBar_JitterSpeed.TickFrequency = 10
            TrackBar_JitterSpeed.Minimum = -450
            TrackBar_JitterSpeed.Maximum = 0
            TrackBar_JitterSpeed.Value = -250
            TrackBar_JitterSpace.TickFrequency = 5
            TrackBar_JitterSpace.Minimum = 5
            TrackBar_JitterSpace.Maximum = 50
            TrackBar_JitterSpace.Value = 25
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
            Timer1.Enabled = CheckBox1.Checked
        End Sub
    
        Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar_JitterSpeed.ValueChanged
            Timer1.Interval = 50 + Math.Abs(TrackBar_JitterSpeed.Value)
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If GetForegroundWindow() <> Me.Handle AndAlso MouseButtons = MouseButtons.Left Then
                If mp = Point.Empty Then mp = MousePosition
                Cursor.Position = New Point(mp.X + R.Next(-TrackBar_JitterSpace.Value, TrackBar_JitterSpace.Value + 1), mp.Y + R.Next(-TrackBar_JitterSpace.Value, TrackBar_JitterSpace.Value + 1))
            Else
                mp = Point.Empty
            End If
        End Sub
    End Class
    
     

     


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Wednesday, May 31, 2017 9:49 PM
    • Marked as answer by bbbbbb32 Wednesday, May 31, 2017 10:59 PM
    Wednesday, May 31, 2017 8:16 PM