How can I get the coordinates of my cursor and the middle of a circle?
I've tried almost everything.
The Circle XAML code:
<Canvas Name="ChooserTime" AllowDrop="True" MouseLeave="ChooserTime_MouseLeave" MouseMove="ChooserTime_MouseMove" MouseUp="ChooserTime_MouseUp" MouseDown="ChooserTime_MouseDown" RenderTransformOrigin="0.5,0.5">
<!-- Some Other Elements Here-->
<!-- Some Other Elements Here-->
<Ellipse Name="MiddleClock" Fill="#FFC35151" Width="2" Height="2"/>
</Canvas>
This is not working:
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);
}
I've tried this too:
public static Point GetMousePositionWindowsForms()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new Point(point.X, point.Y);
}
And this:
[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);
}
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
The angle function:
double angle = Math.Atan((mousePos.Y - MiddleClock.Y) / (mousePos.X -MiddleClock.X));