Joã*_*nço 5 c# wpf windows-runtime winrt-xaml windows-store-apps
我有一个Windows应用商店应用程序,在系统的光标下绘制图像.我使用以下方法捕获所有光标移动:
var window = Window.Current .Content;
window .AddHandler(PointerMovedEvent, new PointerEventHandler (window_PointerMoved), true);
Run Code Online (Sandbox Code Playgroud)
如果我使用鼠标移动光标,这工作正常.
但是,我有另一个应用程序 - 桌面应用程序 - ,它改变了系统光标的位置.我正在使用此方法以编程方式设置光标的位置:
[DllImport("user32")]
private static extern int SetCursorPos(int x, int y);
Run Code Online (Sandbox Code Playgroud)
但是,当以编程方式移动光标时,商店应用程序上的PointerMovedEvent不会触发!有谁知道我怎么能解决这个问题?
我以为我不能在 Windows 商店应用程序上使用 System.Runtime .InteropServices,但这是允许的。因此,我设法通过使用以下方法主动检查光标当前位置的线程来实现所需的行为:
[ DllImport("user32.dll" )]
private static extern bool GetCursorPos(ref Win32Point pt);
Run Code Online (Sandbox Code Playgroud)
这不是最优雅的解决方案,但它确实有效!