WPF等同于unity 3D的Screen.lockCursor

WPF等同于unity 3D的Screen.lockCursor,wpf,unity3d,Wpf,Unity3d,我没有使用Unity 3D,但我想您可以使用Screen.lockCursor控制FPS游戏的鼠标。这在WPF/Win32中可能吗 显然,在退出或发生碰撞时,您必须释放它 谢谢我发现答案分散在一大堆链接上,所以 1设置captureMouse标志,按一次进入此模式,再次出来, 在其中时隐藏光标 bool captureMouse = false; private void viewport3D1_MouseDown(object sender, MouseButtonEventA

我没有使用Unity 3D,但我想您可以使用Screen.lockCursor控制FPS游戏的鼠标。这在WPF/Win32中可能吗

显然,在退出或发生碰撞时,您必须释放它


谢谢

我发现答案分散在一大堆链接上,所以

1设置captureMouse标志,按一次进入此模式,再次出来, 在其中时隐藏光标

    bool captureMouse = false;
    private void viewport3D1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (!captureMouse)
        {
            captureMouse = true;

            Mouse.OverrideCursor = Cursors.None;
        }
        else
        {
            Mouse.OverrideCursor = null;

            captureMouse = false;
        }
    }
2在此模式下,不断将鼠标放回窗口中间

    private void theWindow_MouseMove(object sender, MouseEventArgs e)
    {
        if (!captureMouse)
            return;

        Point windowPoint = WpfToRealPixels(theWindow, new Point(500, 500));
        NativeMethods.SetCursorPos((int)windowPoint.X, (int)windowPoint.Y);
        oldP = new Point(500, 500);
}
翻译合作词

    private Point WpfToRealPixels(Window w, Point p)
    {
        return theWindow.PointToScreen(p);
    }
4要将鼠标放回原位,需要进行本机Win32调用

    public partial class NativeMethods
    {
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool SetCursorPos(int X, int Y);
    }
希望这能帮助别人