title: Shake form on screen
body: <p>I would like to 'shake' my winforms form to provide user feedback, much like the effect used on a lot of mobile OS's.</p>
  
  <p>I can obviously set the location of the window and go back and forth with <code>Form1.Location.X</code> etc. but the effect from this method is terrible. I'd like something a little more fluent - or alternatively is there a way to shake the entire screen? </p>
  
  <p>I'll only be targeting Windows 7 using .net 4.5.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Using both Hans and Vidstige suggestions I've come up with the following, which also works when the window is maximized - I wish I could pick two answers, I've up-voted your answer though Vidstige and hope others will too. Hans' answer hits all the salient points though.</p>
  
  <p>Two forms <code>MainForm</code> and <code>ShakeForm</code></p>
  
  <h2>MainForm Code</h2>
  
  <pre><code> Private Sub shakeScreenFeedback()
  
          Dim f As New Shakefrm
          Dim b As New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppArgb)
  
          Me.DrawToBitmap(b, Me.DisplayRectangle)
  
          f.FormBorderStyle = Windows.Forms.FormBorderStyle.None
          f.Width = Me.Width
          f.Height = Me.Height
          f.ShowInTaskbar = False
  
          f.BackgroundImage = b
          f.BackgroundImageLayout = ImageLayout.Center
          f.Show(Me)
          f.Location = New Drawing.Point(Me.Location.X, Me.Location.Y)
  
          'I found putting the shake code in the formLoad event didn't work
          f.shake()
          f.Close()
  
          b.Dispose()
  
      End Sub
  </code></pre>
  
  <h2>ShakeForm Code</h2>
  
  <pre><code>Public Sub shake()
      Dim original = Location
      Dim rnd = New Random(1337)
      Const shake_amplitude As Integer = 10
      For i As Integer = 0 To 9
          Location = New Point(original.X + rnd.[Next](-shake_amplitude, shake_amplitude), original.Y + rnd.[Next](-shake_amplitude, shake_amplitude))
          System.Threading.Thread.Sleep(20)
      Next
      Location = original
  
  End Sub
  </code></pre>