title: How can I capture mouse events that occur outside of a (WPF) window?
body: <p>I have a <code>Window</code> element that has <code>WindowStyle="None"</code> and <code>AllowsTransparency="True"</code>, therefore it has no title bar and supports transparency.</p>
  
  <p>I want the user to be able to move the Window to any position on the screen by left-clicking anywhere within the Window and dragging. The Window should drag along with the mouse as long as the left mouse button is pressed down.</p>
  
  <p>I was able to get this functionality to work with one exception: when the mouse moves outside of the Window (such as when the left mouse button was pressed down near the edge of the Window and the mouse is moved quicly), the Window no longer captures the mouse position and does not drag along with the mouse.</p>
  
  <p>Here is the code from the code-behind that I use to get the job done:</p>
  
  <pre><code>public Point MouseDownPosition { get; set; }
  public Point MousePosition { get; set; }
  public bool MouseIsDown { get; set; }
  
  private void window_MyWindowName_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
      MouseDownPosition = e.GetPosition(null);
      MouseIsDown = true;
  }
  
  private void window_MyWindowName_MouseMove(object sender, MouseEventArgs e)
  {
      if (MouseIsDown)
      {
          MousePosition = e.GetPosition(null);
          Left += MousePosition.X - MouseDownPosition.X;
          Top += MousePosition.Y - MouseDownPosition.Y;
      }
  }
  
  private void window_MyWindowName_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  {
      MouseIsDown = false;
  }
  </code></pre>