Does anyone know how to simulate mouse drag with SendInput in C++? I want to move the icon on the desktop programmatically. I expected the sequence (Move mouse to A)->(Press left button down)->(Move mouse to B)->(Release left button) would work, but it does not. Mouse moves to A, and then to B, but the icon does not move. I think that Drag'n'Drop (SHDoDragDrop()) is not appropriate for this task - it requires IDataObject and IDropSource to work. Grateful for any hints or ideas.
Asked
Active
Viewed 462 times
-1
-
1Please show the actual code you tried that is not working for you. Alternatively, the Desktop is a [List View](https://learn.microsoft.com/en-us/windows/win32/controls/list-view-control-reference), so you could use `ListView_HitTest()` and `ListView_SetItemPositiion/32()` instead of manipulating the mouse at all. – Remy Lebeau Mar 02 '21 at 19:18
2 Answers
0
Sorry this is not an answer - just the code snippet that I tried to achieve my goal. The goal may be achieved by different means, but I'm interested in this particular one.
INPUT inputs[4] = { 0 };
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dx = (from.x * 65535) / screen_w; inputs[0].mi.dy = (from.y * 65535) / screen_h;
inputs[0].mi.mouseData = 0; inputs[0].mi.time = 0;
inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
inputs[1].type = INPUT_MOUSE;
inputs[1].mi.dx = (from.x * 65535) / screen_w; inputs[1].mi.dy = (from.y * 65535) / screen_h;
inputs[1].mi.mouseData = 0; inputs[1].mi.time = 0;
inputs[1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
inputs[2].type = INPUT_MOUSE;
inputs[2].mi.dx = (to.x * 65535) / screen_w; inputs[2].mi.dy = (to.y * 65535) / screen_h;
inputs[2].mi.mouseData = 0; inputs[2].mi.time = 0;
inputs[2].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
inputs[3].type = INPUT_MOUSE;
inputs[3].mi.dx = (to.x * 65535) / screen_w; inputs[3].mi.dy = (to.y * 65535) / screen_h;
inputs[3].mi.mouseData = 0; inputs[3].mi.time = 0;
inputs[3].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
UINT uSent = SendInput(4, inputs, sizeof(INPUT));

YuriRzhanov
- 1
- 1
0
The answer is easy - SendInput() should be called one by one, for each action. Also, after each call it may be necessary to add Sleep() for a short while. Usually works argument of 10, but sometimes the system needs longer sleep.

YuriRzhanov
- 1
- 1