如何获取相对于屏幕的光标坐标和 WPF 元素坐标?

How to get Cursor Coordinates and WPF element Coordinates with respect to screen?

如何获取光标和圆心的坐标?

我几乎什么都试过了。

圈子XAML代码:

<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>

这不起作用:

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);
}

我也试过这个:

public static Point GetMousePositionWindowsForms()
{
    System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
    return new Point(point.X, point.Y);
}

还有这个:

[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);
}

我知道我的坐标是不是错了,因为圆心和光标之间的角度不应该是这样的

角度函数:

double angle = Math.Atan((mousePos.Y - MiddleClock.Y) / (mousePos.X -MiddleClock.X));

为了获得鼠标在 canvas 上的坐标,您可以使用像

这样的 mousemove 事件
curPoint = e.GetPosition(ChooserTime);

为了得到圆心使用

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

并添加 height/2 和 width/2 以获得中心 `

对于鼠标协调,这对我有用:

添加到SomeName.xaml.cs开头:

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

然后在里面添加

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

在括号内添加此代码 ({}):

/// <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;
}

对于圆形元素(或任何其他 UIElement),这对我有用 [将其添加到括号内 ({})]:

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));
    }
}