在文本框中写入鼠标的绝对位置

本文关键字:位置 鼠标 文本 | 更新日期: 2023-09-27 18:16:02

我有以下问题:

我有两个文本框的窗口。当我点击一个文本框,然后点击其他任何地方(甚至在窗口外),鼠标点击的位置应该写入文本框。

我找到了MouseKeyHook库,其中演示了如何在windows窗体中更新鼠标位置。但我还没有设法将代码应用于我的问题。我甚至不知道我应该把演示中的代码写在哪里。

目前我想到的是:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace LineClicker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void StarttextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            Mouse.Capture(StarttextBox);
            StarttextBox.Text = string.Format(" x {0} , y {1}", PointToScreen(Mouse.GetPosition(this)).X, PointToScreen(Mouse.GetPosition(this)).Y);
        }
    }
}

是一个textBox的代码。当我点击它,x和y坐标显示。它们不是绝对的,我认为这是由于GetPosition方法中的参数this。我必须选择什么来代替this ?

另一件事是位置不总是更新。当我将鼠标移动到桌面的右下角,然后通过点击激活文本框时,位置不会更新。

这里的步骤是什么?

在文本框中写入鼠标的绝对位置

我可以使用Cursor实现这个结果。位置:

一个表示光标在屏幕坐标中的位置。

using System.Windows;
namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var postion = System.Windows.Forms.Cursor.Position;
            textBox.Text = string.Format($"{postion.X}, {postion.Y}");
        }
    }
}

您可以从Microsoft参考源代码中看到Cursor.Position被定义为:

public static Point Position {
    get {
        NativeMethods.POINT p = new NativeMethods.POINT();
        UnsafeNativeMethods.GetCursorPos(p);
        return new Point(p.x, p.y);
    }
    set {
        IntSecurity.AdjustCursorPosition.Demand();
        UnsafeNativeMethods.SetCursorPos(value.X, value.Y);
    }
}

所以就像在yan yankelevich的回答,它仍然使用SetCursorPos,但这样更容易调用。

除此之外,它可能只是取决于您是否乐意包含对System.Windows.Forms的引用

首先,您需要获得鼠标的绝对位置(不是相对于窗口或控件的位置)。为此,您需要以下选项之一(从这里:https://stackoverflow.com/a/4232281/4664754):

  • 通过在项目中添加对System.Windows.Forms的引用(在解决方案资源管理器中的References中->右键单击->添加引用-> Assemblys-> Framework ->勾选System.Windows.Forms附近的框)。然后将这个静态函数添加到某个类中(我们称之为MouseHelper.cs):

    public static Point GetMousePositionWindowsForms()
    {
        System.Drawing.Point point = Control.MousePosition;
        return new Point(point.X, point.Y);
    }
    
  • 通过粘贴以下代码到mainwindow . example .cs:

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

无论你选择哪种方式,你都需要在你的OnFocusChanged中调用这些函数之一:

 private void StarttextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            Mouse.Capture(StarttextBox);
            Point mouseCoord = MouseHelper.GetMousePositionWindowsForms();
            // Or if you choose the other way :
            //Point mouseCoord = GetMousePosition();
            StarttextBox.Text = string.Format(" x {0} , y {1}", mouseCoord.X, mouseCoord .Y);
        }

这样坐标应该是正确的。对于你的坐标没有在正确的时间显示的问题,我认为你的重点解决方案不是你要找的。

您应该尝试实现如下内容:https://stackoverflow.com/a/2064009/4664754并在每次调用TheMouseMoved事件时更改您的文本框值