I want to be able to access the coordinates of the mouse whether or not the cursor is over the window of my application.
When I use Mouse.Capture(IInputElement) or UIElement.CaptureMouse(), both fail to capture the mouse and return false.
What might be my problem?
The cs file for my window is as follows:
using System.Windows;
using System.Windows.Input;
namespace ScreenLooker
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
bool bSuccess = Mouse.Capture(this);
bSuccess = this.CaptureMouse();
}
protected override void OnMouseMove(MouseEventArgs e)
{
tbCoordX.Text = e.GetPosition(this).X.ToString();
tbCoordY.Text = e.GetPosition(this).Y.ToString();
//System.Drawing.Point oPoint = System.Windows.Forms.Cursor.Position;
//tbCoordX.Text = oPoint.X.ToString();
//tbCoordY.Text = oPoint.Y.ToString();
base.OnMouseMove(e);
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…