如何获取鼠标在屏幕上的当前坐标?我只知道 Mouse.GetPosition()
可以获取元素的鼠标位置,但我想要不使用元素获取坐标。
如何获取鼠标在屏幕上的当前坐标?我只知道 Mouse.GetPosition()
可以获取元素的鼠标位置,但我想要不使用元素获取坐标。
或者在纯WPF中使用PointToScreen。
以下是示例帮助方法:
// Gets the absolute mouse position, relative to screen
Point GetMousePos() => _window.PointToScreen(Mouse.GetPosition(_window));
在Rachel的回答之后,这里有两种方法可以在WPF中获取鼠标屏幕坐标。
1.使用Windows Forms。 添加对System.Windows.Forms的引用。
public static Point GetMousePositionWindowsForms()
{
var point = Control.MousePosition;
return new Point(point.X, point.Y);
}
2.使用Win32
[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()
{
var w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
return new Point(w32Mouse.X, w32Mouse.Y);
}
您想要相对于屏幕还是应用程序的坐标?
如果是在应用程序内部,只需使用:
Mouse.GetPosition(Application.Current.MainWindow);
如果没有的话,我相信你可以添加对 System.Windows.Forms
的引用,然后使用:
System.Windows.Forms.Control.MousePosition;
error CS0234: The type or namespace name 'Control' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)
- Jonathan Tuzman如果您在不同的分辨率、使用多个显示器的电脑上尝试了很多这些答案,您可能会发现它们无法可靠地工作。这是因为您需要使用转换来获取相对于当前屏幕的鼠标位置,而不是整个视图区域(包括所有显示器)。类似于这样的代码...(其中“this”是一个WPF窗口)。
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var mouse = transform.Transform(GetMousePosition());
public System.Windows.Point GetMousePosition()
{
var point = Forms.Control.MousePosition;
return new Point(point.X, point.Y);
}
using System.Windows;
using System.Windows.Input;
/// <summary>
/// Gets the current mouse position on screen
/// </summary>
private Point GetMousePosition()
{
// Position of the mouse relative to the window
var position = Mouse.GetPosition(Window);
// Add the window position
return new Point(position.X + Window.Left, position.Y + Window.Top);
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT pPoint);
Point是一个轻量级的结构体
,它只包含X和Y字段。
public MainWindow()
{
InitializeComponent();
DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(timer_tick);
dt.Interval = new TimeSpan(0,0,0,0, 50);
dt.Start();
}
private void timer_tick(object sender, EventArgs e)
{
POINT pnt;
GetCursorPos(out pnt);
current_x_box.Text = (pnt.X).ToString();
current_y_box.Text = (pnt.Y).ToString();
}
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
这个解决方案还可以解决参数读取过于频繁或不够频繁的问题,因此您可以自己进行调整。但是请记住,WPF方法过载只接受一个表示ticks
而不是milliseconds
的参数。
TimeSpan(50); //ticks
new Point(Mouse.GetPosition(mWindow).X + mWindow.Left, Mouse.GetPosition(mWindow).Y + mWindow.Top)
< p > + mWindow.Left
和 + mWindow.Top
确保当用户拖动窗口时,位置仍然正确。
Mouse.GetPosition(mWindow)
会给你相对于你选择的参数的鼠标位置。
mWindow.PointToScreen()
将该位置转换为相对于屏幕的点。
因此,假设mWindow
是一个窗口(实际上,任何继承自System.Windows.Media.Visual
类的类都将具有此函数),mWindow.PointToScreen(Mouse.GetPosition(mWindow))
会给出相对于屏幕的鼠标位置。如果您在WPF窗口类中使用此函数,则this
应该有效。
我想使用这段代码
Point PointA;
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e) {
PointA = e.MouseDevice.GetPosition(sender as UIElement);
}
private void Button_Click(object sender, RoutedEventArgs e) {
// use PointA Here
}
System.Windows.Forms.Cursor.Position
- vinsa