我想獲得相對於屏幕座標的鼠標位置。我正在使用下面的代碼來做到這一點。鼠標移動不觸發外部WPF主窗口
window.PointToScreen(Mouse.GetPosition(window));
它工作正常。但是我的MouseMove事件不會在MainWindow之外觸發。這是如果我將我的鼠標移動到桌面上並恢復了我的窗口。
任何想法表示讚賞。
我想獲得相對於屏幕座標的鼠標位置。我正在使用下面的代碼來做到這一點。鼠標移動不觸發外部WPF主窗口
window.PointToScreen(Mouse.GetPosition(window));
它工作正常。但是我的MouseMove事件不會在MainWindow之外觸發。這是如果我將我的鼠標移動到桌面上並恢復了我的窗口。
任何想法表示讚賞。
使用CaptureMouse()方法。
對於你上面的例子,你可以添加:
window.CaptureMouse();
在您的代碼隱藏MouseDown事件處理程序中。
然後你需要調用:
window.ReleaseMouseCapture();
在您的代碼隱藏MouseUp事件處理程序中。
我需要能夠捕捉WPF窗口之外的鼠標位置,而不管按下任意鼠標按鈕的。我最終使用Interop來調用與線程結合的WINAPI GetCursorPos,而不是窗口的事件。
using System.Runtime.InteropServices;
using Point = System.Drawing.Point;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(ref Point lpPoint);
public MainWindow()
{
InitializeComponent();
new Thread(() =>
{
while (true)
{
//Logic
Point p = new Point();
GetCursorPos(ref p);
//Update UI
Dispatcher.BeginInvoke(new Action(() =>
{
Position.Text = p.X + ", " + p.Y;
}));
Thread.Sleep(100);
}
}).Start();
}
}
很棒!
非常有幫助!謝謝 – DevEstacion 2013-10-02 11:47:10