title: How to test if a window is being dragged C# WPF
body: <p>I have a title-less window, as I wanted to create a window style of my own.</p>
  
  <p>The title and minimize, maximize and close buttons are in a dock panel. I have added the following event handler to maximize, restore and drag the window.</p>
  
  <p>The problem comes when the window is maximized.</p>
  
  <p>What I have found is that whenever I do a single click on the title is restores. When I only want it to restore if it is double clicked or dragged. I can see why it is happening but unsure how to resolve this.</p>
  
  <pre><code>    public void TITLEBAR_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          DockPanel dp = (DockPanel)sender;
          Window parentWindow = Window.GetWindow(dp);
          bool doubleClick = IsDoubleClick(sender, e);
  
          if (e.ChangedButton == MouseButton.Left &amp;&amp; e.LeftButton == MouseButtonState.Pressed &amp;&amp; !doubleClick)
          {
  
  
              if (parentWindow.WindowState == WindowState.Maximized)
              {
                  double mouseX = e.GetPosition(parentWindow).X;
                  double width = parentWindow.RestoreBounds.Width;
                  System.Drawing.Rectangle screenBounds = getCurrentScreenBounds(parentWindow);
                  double x = screenBounds.Left + (mouseX - ((width / 100.00) * ((100.00 / screenBounds.Width) * mouseX)));
  
                  if (x &lt; 0)
                  {
                      x = 0;
                  }
                  else
                  {
                      if (x + width &gt; screenBounds.Left + screenBounds.Width)
                      {
                          x = screenBounds.Left + screenBounds.Width - width;
                      }
                  }
  
                  parentWindow.Left = x;
                  parentWindow.Top = screenBounds.Top;
                  parentWindow.WindowState = System.Windows.WindowState.Normal;
              }
  
              parentWindow.DragMove();
              //MessageBox.Show("");
          }
  
          if (doubleClick)
          {
              if (parentWindow.WindowState == System.Windows.WindowState.Maximized)
              {
                  parentWindow.WindowState = System.Windows.WindowState.Normal;
              }
              else
              {
                  parentWindow.WindowState = System.Windows.WindowState.Maximized;
              }
          }
      }
  </code></pre>
  
  <p>Along with this class:</p>
  
  <pre><code>public static class MouseButtonHelper
  {
      private const long k_DoubleClickSpeed = 500;
      private const double k_MaxMoveDistance = 10;
  
      private static long _LastClickTicks = 0;
      private static System.Windows.Point _LastPosition;
      private static WeakReference _LastSender;
  
      public static bool IsDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
      {
          System.Windows.Point position = e.GetPosition(null);
          long clickTicks = DateTime.Now.Ticks;
          long elapsedTicks = clickTicks - _LastClickTicks;
          long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond;
          bool quickClick = (elapsedTime &lt;= k_DoubleClickSpeed);
          bool senderMatch = (_LastSender != null &amp;&amp; sender.Equals(_LastSender.Target));
  
          if (senderMatch &amp;&amp; quickClick &amp;&amp; position.Distance(_LastPosition) &lt;= k_MaxMoveDistance)
          {
              // Double click!
              _LastClickTicks = 0;
              _LastSender = null;
              return true;
          }
  
          // Not a double click
          _LastClickTicks = clickTicks;
          _LastPosition = position;
          if (!quickClick)
              _LastSender = new WeakReference(sender);
          return false;
      }
  
  
      private static double Distance(this System.Windows.Point pointA, System.Windows.Point pointB)
      {
          double x = pointA.X - pointB.X;
          double y = pointA.Y - pointB.Y;
          return Math.Sqrt(x * x + y * y);
      }
  }
  </code></pre>
  
  <p>And this to work out bounds of the current screen.</p>
  
  <pre><code>public static class WindowHelper
  {
      public static System.Drawing.Rectangle getCurrentScreenBounds(System.Windows.Window pWnd)
      {
          System.Windows.Forms.Screen parentScreen = GetCurrentScreen(pWnd);
  
          if (parentScreen == null)
          {
              return System.Windows.Forms.Screen.PrimaryScreen.Bounds;
          }
  
          return parentScreen.Bounds;
      }
  
      private static System.Windows.Forms.Screen GetCurrentScreen(System.Windows.Window pWnd)
      {
          System.Drawing.Rectangle intersectingRect = new System.Drawing.Rectangle();
          System.Drawing.Rectangle windowRect = new System.Drawing.Rectangle(Convert.ToInt32(pWnd.Left), Convert.ToInt32(pWnd.Top), Convert.ToInt32(pWnd.Width), Convert.ToInt32(pWnd.Height));
          int largestIntersectingArea = 0;
          System.Windows.Forms.Screen curScreen = null;
  
          foreach (System.Windows.Forms.Screen s in System.Windows.Forms.Screen.AllScreens)
          {
              if (s.Bounds.IntersectsWith(windowRect))
              {
                  intersectingRect = System.Drawing.Rectangle.Intersect(s.Bounds, windowRect);
                  int intersectingArea = intersectingRect.Width * intersectingRect.Height;
                  if (intersectingArea &gt; largestIntersectingArea)
                  {
                      largestIntersectingArea = intersectingArea;
                      curScreen = s;
                  }
              }
          }
  
          return curScreen;
      }
  }
  </code></pre>