WPF相当于Unity 3D的Screen.lockCursor

时间:2015-01-24 13:09:40

标签: wpf unity3d

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

显然你必须在退出或发生崩溃时释放它

由于

1 个答案:

答案 0 :(得分:0)

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

(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);
}

(3)翻译合作伙伴

    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);
    }

希望有人帮助。