title: How to get Cursor Coordinates and WPF element Coordinates with respect to screen?
body: <p>How can I get the coordinates of my cursor and the middle of a circle?</p>
  
  <p>I've tried almost everything.</p>
  
  <p>The Circle XAML code:</p>
  
  <pre><code>&lt;Canvas Name="ChooserTime" AllowDrop="True" MouseLeave="ChooserTime_MouseLeave" MouseMove="ChooserTime_MouseMove" MouseUp="ChooserTime_MouseUp" MouseDown="ChooserTime_MouseDown" RenderTransformOrigin="0.5,0.5"&gt;
      &lt;!-- Some Other Elements Here--&gt;
      &lt;!-- Some Other Elements Here--&gt;
      &lt;Ellipse Name="MiddleClock" Fill="#FFC35151" Width="2" Height="2"/&gt;
  &lt;/Canvas&gt;
  </code></pre>
  
  <p>This is not working:</p>
  
  <pre><code>private void ChooserTime_MouseMove(object sender, MouseEventArgs e)
  {
      MessageBox.Show("move");
      System.Threading.Thread.Sleep(2000); // For Have Time to Moving My Mouse
  
      // This will get the mouse cursor relative to the upper left corner of your ellipse.
      // Note that nothing will happen until you are actually inside of your ellipse.
      Point curPoint = e.GetPosition(MiddleClock);
  
      // Assuming that your ellipse is actually a circle.
      Point center = new Point(MiddleClock.Width / 2, MiddleClock.Height / 2);
  
      // A bit of math to relate your mouse to the center...
      Point relPoint = new Point(curPoint.X - center.X, curPoint.Y - center.Y);
  }
  </code></pre>
  
  <p>I've tried this too:</p>
  
  <pre><code>public static Point GetMousePositionWindowsForms()
  {
      System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
      return new Point(point.X, point.Y);
  }
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>[DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  internal static extern bool GetCursorPos(ref Win32Point pt);
  
  [StructLayout(LayoutKind.Sequential)]
  internal struct Win32Point
  {
      public Int32 X;
       public Int32 Y;
  };
  
  public static Point GetMousePosition()
  {
      Win32Point w32Mouse = new Win32Point();
      GetCursorPos(ref w32Mouse);
      return new Point(w32Mouse.X, w32Mouse.Y);
  }
  </code></pre>
  
  <p>I know if I'm wrong about the coordinates because the angle between the middle of the circle and the cursor is not how it should be</p>
  
  <p>The angle function:</p>
  
  <pre><code>double angle = Math.Atan((mousePos.Y - MiddleClock.Y) / (mousePos.X -MiddleClock.X));
  </code></pre>