windows 处理鼠标拖动的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6142206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:56:33  来源:igfitidea点击:

What is the proper way of handling a mouse drag?

c++windows

提问by Mike Morum

I need to implement mouse drag events which look something like this:

我需要实现如下所示的鼠标拖动事件:

class MouseDragEvent
{
public:
   uint m_btn;
   uint m_x, m_y;
   uint m_delta_x, m_delta_y;
};

I think I will need to check for WM_LBUTTONDOWN and WM_LBUTTONUP messages and manually find the change in x and y. Is there a drag message or a better way?

我想我需要检查 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息并手动找到 x 和 y 的变化。有没有拖拽消息或者更好的方法?

回答by Mark Ransom

Start by detecting WM_LBUTTONDOWN. Record the starting coordinates where the mouse button was pressed. Check for WM_MOUSEMOVE, and when the mouse has moved outside the rectangle determined by GetSystemParameters(SM_CXDRAG)and GetSystemParameters(SM_CYDRAG)use SetCaptureto capture the mouse. At this point continue responding to WM_MOUSEMOVEand check for WM_LBUTTONUP. You might want to change the mouse cursor at this point. Also check for WM_CAPTURECHANGED, which means the drag has been aborted. After the drag is complete call ReleaseCapture.

从检测开始WM_LBUTTONDOWN。记录按下鼠标按钮的起始坐标。检查WM_MOUSEMOVE, 以及当鼠标移到由GetSystemParameters(SM_CXDRAG)GetSystemParameters(SM_CYDRAG)用于SetCapture捕获鼠标所确定的矩形之外时。此时继续响应WM_MOUSEMOVE并检查WM_LBUTTONUP。此时您可能想要更改鼠标光标。还要检查WM_CAPTURECHANGED,这意味着拖动已中止。拖动完成后调用ReleaseCapture

Edit:Most of this process can be automated with the DragDetectfunction. Call this function from the WM_LBUTTONDOWN handler.

编辑:这个过程的大部分都可以通过该DragDetect功能实现自动化。从 WM_LBUTTONDOWN 处理程序调用此函数。

回答by Ben Voigt

There are drag-and-drop APIs in Windows (e.g. RegisterDragDrop), but concerned with the from- and to- windows (often in different applications), not the coordinates.

Windows 中有拖放 API(例如RegisterDragDrop),但关注的是 from- 和 to- 窗口(通常在不同的应用程序中),而不是坐标。

If you want to deal with delta-x and delta-y coordinated, then processing button down and button up messages is appropriate.

如果你想处理 delta-x 和 delta-y 协调,那么处理 button down 和 button up 消息是合适的。