![](/img/trans.png)
[英]Drag and drop an to TextBox in a specific mouse position - Show caret or position indicator
[英]Write absolute mouse position in TextBox
我有以下问题:
我有两个文本框的窗口。 当我单击文本框然后单击其他任何地方(甚至在窗口外)时,鼠标单击位置应写入文本框。
我找到了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);
}
}
}
这是一个文本框的代码。 当我点击它时,会显示 x 和 y 坐标。 它们不是绝对的,我认为这是由于GetPosition
方法中的参数this
GetPosition
。 我必须选择什么而不是this
?
另一件事是位置并不总是更新。 当我将鼠标移动到桌面的右下角,然后通过按 Tab 键激活文本框时,位置不会更新。
这里的步骤是什么?
我能够使用Cursor.Position实现这个结果:
一个Point表示光标在屏幕坐标中的位置。
例子
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}");
}
}
}
从微软的参考资料中可以看出, 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 的引用(进入解决方案资源管理器中的引用 -> 右键单击 -> 添加引用 -> 程序集 -> 框架 -> 勾选 System.Windows.Forms 附近的框)。 然后在某个类中添加这个静态函数(我们称之为 MouseHelper.cs):
public static Point GetMousePositionWindowsForms() { System.Drawing.Point point = Control.MousePosition; return new Point(point.X, point.Y); }
通过将此代码粘贴到 MainWindow.xaml.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 事件时更改您的文本框值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.