简体   繁体   English

使用 SendInput C++ 模拟拖动

[英]Simulating drag with SendInput C++

Does anyone know how to simulate mouse drag with SendInput in C++?有谁知道如何在 C++ 中使用 SendInput 模拟鼠标拖动? 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.我希望序列(将鼠标移至 A)->(按下左键)->(将鼠标移至 B)->(释放左键)会起作用,但事实并非如此。 Mouse moves to A, and then to B, but the icon does not move.鼠标移动到 A,然后移动到 B,但图标没有移动。 I think that Drag'n'Drop (SHDoDragDrop()) is not appropriate for this task - it requires IDataObject and IDropSource to work.我认为 Drag'n'Drop (SHDoDragDrop()) 不适合此任务 - 它需要 IDataObject 和 IDropSource 才能工作。 Grateful for any hints or ideas.感谢任何提示或想法。

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

The answer is easy - SendInput() should be called one by one, for each action.答案很简单——对于每个动作,SendInput() 都应该被一一调用。 Also, after each call it may be necessary to add Sleep() for a short while.此外,在每次调用之后,可能需要在短时间内添加 Sleep()。 Usually works argument of 10, but sometimes the system needs longer sleep.通常工作参数为 10,但有时系统需要更长的睡眠时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM