我从网格中捕获了鼠标位置,并放入了TextBlock:
XAML:
<Grid MouseMove="Grid_MouseMove" SizeChanged="MainGrid_SizeChanged">
<TextBlock x:Name="tbMouse_X" HorizontalAlignment="Left" Margin="66,31,0,0" VerticalAlignment="Top/>
<TextBlock x:Name="tbMouse_Y" HorizontalAlignment="Left" Margin="66,61,0,0" VerticalAlignment="Top"/>
</Grid>
C#:
private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
fp_Show_Mouse_Position();
}
public void fp_Show_Mouse_Position()
{
tbMouse_X.Text = Mouse.GetPosition(this).X.ToString();
tbMouse_Y.Text = Mouse.GetPosition(this).Y.ToString();
}
它正在工作,但我想从整个屏幕上捕获位置。我该怎么办?
关于System.Windows.Forms.Cursor.Position有一些线索,但是我想使用System.Windows.Input.Mouse。
答案 0 :(得分:0)
您可以使用某些P / Invoke捕获整个屏幕上的鼠标位置:
public partial class MainWindow : Window
{
private const int WH_MOUSE_LL = 14;
private IntPtr _mouseHandle;
private delegate IntPtr HookDelegate(int Code, IntPtr wParam, IntPtr lParam);
private HookDelegate _mouseDelegate;
[DllImport("User32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, HookDelegate lpfn, IntPtr hmod, int dwThreadId);
[DllImport("User32.dll")]
static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll")]
static extern IntPtr UnhookWindowsHookEx(IntPtr hHook);
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public int X;
public int Y;
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out POINT lpPoint);
public MainWindow()
{
InitializeComponent();
_mouseDelegate = MouseHookDelegate;
_mouseHandle = SetWindowsHookEx(WH_MOUSE_LL, _mouseDelegate, IntPtr.Zero, 0);
Closing += (s, e) =>
{
if (_mouseHandle != IntPtr.Zero)
UnhookWindowsHookEx(_mouseHandle);
};
}
private IntPtr MouseHookDelegate(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0)
return CallNextHookEx(_mouseHandle, code, wParam, lParam);
POINT point;
GetCursorPos(out point);
tbMouse_X.Text = point.X.ToString();
tbMouse_Y.Text = point.Y.ToString();
return CallNextHookEx(_mouseHandle, code, wParam, lParam);
}
}
答案 1 :(得分:0)
这是基于此answer的工作代码:
我还增加了一个功能:将光标移动到随机位置。
XAML :
<Grid>
<TextBlock x:Name="tbMouse_X" HorizontalAlignment="Left" Margin="66,31,0,0" VerticalAlignment="Top/>
<TextBlock x:Name="tbMouse_Y" HorizontalAlignment="Left" Margin="66,61,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="tbMouse_A" HorizontalAlignment="Left" Margin="206,31,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="tbMouse_B" HorizontalAlignment="Left" Margin="206,61,0,0" VerticalAlignment="Top"/>
</Grid>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(timer_tick);
dt.Tick += new EventHandler(move_mouse);
dt.Interval = new TimeSpan(0, 0, 0, 0, 10000);
dt.Start();
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT pPoint);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int new_x, int new_y);
private void timer_tick(object sender, EventArgs e)
{
POINT pnt;
GetCursorPos(out pnt);
tbMouse_X.Text = (pnt.X).ToString();
tbMouse_Y.Text = (pnt.Y).ToString();
}
public void move_mouse(object sender, EventArgs e)
{
Random rnd = new Random();
int A = rnd.Next(0, 1000);
int B = rnd.Next(0, 1000);
SetCursorPos(A, B);
tbMouse_A.Text = A.ToString();
tbMouse_B.Text = B.ToString();
}
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}