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



如何获取光标和圆圈中间的坐标?

我几乎尝试了所有方法。

圆形 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));

为了在画布上获取鼠标的坐标,您可以使用鼠标移动事件,例如

curPoint = e.GetPosition(ChooserTime);

为了得到圆心使用

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

并添加高度/2 和宽度/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));
}
}

最新更新