What is the correct way to drag and drop?

I need to implement mouse drag events that 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 the WM_LBUTTONDOWN and WM_LBUTTONUP messages and manually find the changes in x and y. Is there a forwarding message or a better way?

+6
source share
3 answers

Start by discovering WM_LBUTTONDOWN . Record the starting coordinates at which the mouse button was clicked. Check WM_MOUSEMOVE , and when the mouse has moved outside the rectangle defined by GetSystemParameters(SM_CXDRAG) and GetSystemParameters(SM_CYDRAG) , use SetCapture to capture the mouse. At this point, continue to respond to WM_MOUSEMOVE and check WM_LBUTTONUP . You can change the mouse cursor at this moment. Also check WM_CAPTURECHANGED , which means the drag has been interrupted. After the drag is completed, call ReleaseCapture .

Edit:. Most of these processes can be automated using the DragDetect function. Call this function from the WM_LBUTTONDOWN handler.

+6
source

Windows has drag-and-drop APIs (e.g. RegisterDragDrop ), but associated with windows and windows (often in different applications), and not with coordinates.

If you want to work with the coordinates delta-x and delta-y, then the message of the button "down" and "button up" is suitable.

0
source

Source: https://habr.com/ru/post/889135/


All Articles