gpt4 book ai didi

c++ - 在窗口中捕获光标

转载 作者:可可西里 更新时间:2023-11-01 09:44:40 26 4
gpt4 key购买 nike

我使用 DX11 在 C++ 中编写了自己的相机类。目前我使用 WM_MOUSEMOVE 事件在场景中环顾四周。为了防止光标离开窗口,每当发生 WM_MOUSEMOVE 事件时,我都会调用函数 SetCursorPos 使鼠标居中。但是,如果我非常快地移动鼠标,光标就会离开窗口。一个解决方案是使用函数 ClipCursor,但这会导致当光标碰到矩形的边界时相机的旋转变得不稳定。所以 ClipCursor 解决了原来的问题,却以另一个结束。

你们有什么解决办法吗?

最佳答案

对于 Windows 桌面应用程序,只需使用 ClipCursor给出窗口的矩形:

    RECT rect;
GetClientRect(mWindow, &rect);

POINT ul;
ul.x = rect.left;
ul.y = rect.top;

POINT lr;
lr.x = rect.right;
lr.y = rect.bottom;

MapWindowPoints(mWindow, nullptr, &ul, 1);
MapWindowPoints(mWindow, nullptr, &lr, 1);

rect.left = ul.x;
rect.top = ul.y;

rect.right = lr.x;
rect.bottom = lr.y;

ClipCursor(&rect);

确保有办法退出此模式,以便用户可以根据需要选择与其他窗口进行交互。通常,当您转到“暂停”菜单以摆脱这种“鼠标外观”行为时,您会调用 ClipCursor(nullptr);

您也可以在桌面应用程序中使用“原始”输入,请参阅 Taking Advantage of High-Definition Mouse Movement .请记住,原始输入对于相对移动的“鼠标外观”行为非常有效,但是 (a) 它不适用于远程桌面,并且 (b) 您不会获得“指针弹道”,这是一个使鼠标更灵活的非线性移动速率,因此在处理绝对移动时,您通常应该坚持使用传统的 WM_MOUSE 消息。

For Universal Windows Platform, you can't use "raw" input as there's no equivalent to WM_INPUT, but you do get high-precision data out of the MouseDevice.MouseMoved event via the MouseDelta property. You don't need to use ClipCursor for relative movement in UWP, just turning off the cursor by setting the CoreWindow.PointerCursor property to nullptr will prevent the system mouse position from being tracked. Again, you should restore the system cursor when you are in a 'pause' menu. See Developing mouse controls (DirectX and C++).

参见 DirectX 工具包 Mouse辅助类,更重要的是 implementation文件。对于相对模式,它使用 ClipCursorWM_INPUT 来实现 Windows 桌面 Win32 实现。

关于c++ - 在窗口中捕获光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36779161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com