我几乎没有新发现,
代码如下,首先构建并运行 Window ,
然后只需在窗口上滚动一次鼠标即可调用鼠标位置的无限屏幕检测。
(因此,在问题的第二点中,我没有找到检测鼠标事件失控的方法,但类似地使用了无限线程。)
但是我只是在WPF项目中使用了一点技巧来启用Windows.Forms,只需在纯方法中使用Forms代码,然后在事件代码块中引用该方法。
.
这是代码:
添加两个对项目的引用:
System.Drawing
System.Windows.Forms
Xaml 部分:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:g="clr-namespace:Gma.UserActivityMonitor;assembly=Gma.UserActivityMonitor"
Title="MainWindow" Height="350" Width="525"
MouseWheel="MainWindow_OnMouseWheel">
<Grid>
<TextBlock Name="TBK" />
</Grid>
</Window>
班级代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void KeepReportMousePos()
{
//Endless Report Mouse position
Task.Factory.StartNew(() =>
{
while(true){
this.Dispatcher.Invoke(
DispatcherPriority.SystemIdle,
new Action(() =>
{
GetCursorPos();
}));
}
});
}
public void GetCursorPos()
{
//get the mouse position and show on the TextBlock
System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
TBK.Text = p.X + " " + p.Y;
}
private void MainWindow_OnMouseWheel(object sender, MouseWheelEventArgs e)
{
//invoke mouse position detect when wheel the mouse
KeepReportMousePos();
}
}