-1

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));
Ramankingdom
  • 1,478
  • 2
  • 12
  • 17
Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36

2 Answers2

1

In order to get the Coordinates of the mouse on canvas you can use mousemove event like

curPoint = e.GetPosition(ChooserTime);

In order to get the center of circle use

Canvas.GetTop(MiddleClock) and canvas.GetLeft(MiddleClock)

and add the height/2 and width/2 to get the center `

Ramankingdom
  • 1,478
  • 2
  • 12
  • 17
  • I want the coordinates of the mouse anywhere on the screen related to the circle – Raz Luvaton Jul 19 '17 at 08:13
  • 1
    @RazLuvaton hope this article will give you an idea. https://stackoverflow.com/questions/17009075/c-sharp-cursor-position-all-screen You need to import system.wimdows.forms dll in this case – Ramankingdom Jul 19 '17 at 09:41
1

For the mouse coordination this worked for me:

Add to the beginning of the SomeName.xaml.cs:

using System;
using System.Runtime.InteropServices; // Add this

And then add inside

public partial class Name : Somethingelse
{
   // Add here the code below
}

Add this code inside the brackets ({}):

/// <summary>
/// Struct representing a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public static implicit operator Point(POINT point)
    {
        return new Point(point.X, point.Y);
    }
}

/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

public static Point GetCursorPosition()
{
    POINT lpPoint;
    GetCursorPos(out lpPoint);

    return lpPoint;
}

For the circle element (or any other UIElement), this worked for me [Add this inside the brackets ({})]:

public static Point ElementPointToScreenPoint(UIElement element, Point pointOnElement)
{
    return element.PointToScreen(pointOnElement);
}

private void OnElementMouseDown(object sender, MouseButtonEventArgs e)
{
    if (MiddleClock is UIElement)
    {
        Point MiddleClockCor = ElementPointToScreenPoint(MiddleClock as UIElement, new Point(0, 0));
    }
}
Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
  • 1
    Its nice to find the exact answer. You are great bro!. I just did a little research but you dig deep into the roots. awesome. – Ramankingdom Jul 20 '17 at 06:45