如何获取光标的坐标和圆圈的中间?
我几乎尝试过所有事情。
Circle 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));
答案 0 :(得分:1)
为了在画布上获得鼠标的坐标,您可以使用像
这样的mousemove事件curPoint = e.GetPosition(ChooserTime);
为了获得圆圈的中心
Canvas.GetTop(MiddleClock) and canvas.GetLeft(MiddleClock)
并添加高度/ 2和宽度/ 2以获得中心`
答案 1 :(得分:1)
对于鼠标协调,这对我有用:
添加到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;
}
对于circle元素(或任何其他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));
}
}