Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

How to automate drag&drop using SendInput

$
0
0

I want to test a flash application running on browser. I need to drag pieces around in the application. I want to do this by simulating mouse movements using windows api.

Here is my navie solution to simulate a mouse drag:

				void MoveMouse(LPINPUT input, int x, int y) {
					input->type = INPUT_MOUSE;
					input->mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));
					input->mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));
					input->mi.dwFlags =  MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
				}

				void LeftDownMouse(LPINPUT input, int x, int y) {
					input->type = INPUT_MOUSE;
					input->mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));
					input->mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));
					input->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
				}

				void LeftUpMouse(LPINPUT input, int x, int y) {
					input->type = INPUT_MOUSE;
					input->mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));
					input->mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));
					input->mi.dwFlags = MOUSEEVENTF_LEFTUP;
				}

				void DragMouse(LPINPUT inputs, int startX, int startY, int dropX, int dropY) {
					MoveMouse(&inputs[0], startX, startY);
					LeftDownMouse(&inputs[1], startX, startY);
					LeftUpMouse(&inputs[2], startX, startY);
					LeftDownMouse(&inputs[3], startX, startY);
					MoveMouse(&inputs[4], dropX, dropY);
					LeftUpMouse(&inputs[5], dropX, dropY);
				}

				int _tmain(int argc, _TCHAR* argv[])
				{
					INPUT inputs [6] = {};
                                        // Assume there is a maximized window, mouse drags the window from 40, 10 to 100,100 coordinates.
					DragMouse(inputs, 40, 10, 100, 100);

					SendInput(6, inputs, sizeof(INPUT));


					Sleep(1000);

					return 0;
				}



This code mostly doesn't work, sometimes works, it has undefined behaviour. What could be the problem? How can i solve this?


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>