Google Answers Logo
View Question
 
Q: Simulate Mouse Move in C++ ( No Answer,   3 Comments )
Question  
Subject: Simulate Mouse Move in C++
Category: Computers > Programming
Asked by: alex130-ga
List Price: $2.00
Posted: 11 Jun 2004 08:20 PDT
Expires: 15 Jun 2004 11:22 PDT
Question ID: 359697
Hi,
I'm using windows XP and trying to simulate mouse movement and mouse
clicks. The following code supposed to move mouse to absolute position
(100,100) and perform a click:

//test.cpp file:

#include "stdafx.h"

int main(int argc, char* argv[]){
INPUT *buffer = new INPUT[3]; //allocate a buffer
buffer->type = INPUT_MOUSE;
buffer->mi.dx = 100;
buffer->mi.dy = 100;
buffer->mi.mouseData = 0;
buffer->mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);
buffer->mi.time = 0;
buffer->mi.dwExtraInfo = 0;

(buffer+1)->type = INPUT_MOUSE;
(buffer+1)->mi.dx = 100;
(buffer+1)->mi.dy = 100;
(buffer+1)->mi.mouseData = 0;
(buffer+1)->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
(buffer+1)->mi.time = 0;
(buffer+1)->mi.dwExtraInfo = 0;

(buffer+2)->type = INPUT_MOUSE;
(buffer+2)->mi.dx = 100;
(buffer+2)->mi.dy = 100;
(buffer+2)->mi.mouseData = 0;
(buffer+2)->mi.dwFlags = MOUSEEVENTF_LEFTUP;
(buffer+2)->mi.time = 0;
(buffer+2)->mi.dwExtraInfo = 0;


SendInput(3,buffer,sizeof(INPUT));
delete (buffer); //clean up our messes.
return 0;
}

when "stdafx.h" is:

#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define _WIN32_WINNT 0x0500 // so the code would compile
#include <windows.h>


The problem is that the mouse moves to position (0,0) and performs a
click. I've been unable so far to simulate mouse movement to any
absolute coordinate (x,y) specified in mi.dx and mi.dy respectively.
If someone knows a way to force mouse to move to absolute position
(x,y) please tell me...

Here is the link to source of this code in visual C++ 6.0 project:
http://www.angelfire.com/games5/matrixalg/test.zip
Answer  
There is no answer at this time.

Comments  
Subject: Re: Simulate Mouse Move in C++
From: drip-ga on 11 Jun 2004 10:28 PDT
 
Not that this has anything to do with your question, but the
second-to-last line in main should be:
  delete [] buffer;

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_31.asp
for more details. Not using the correct delete syntax could have bad
effects.
Subject: Re: Simulate Mouse Move in C++
From: politicalguru-ga on 11 Jun 2004 10:48 PDT
 
Thank you for your question.  
 
However, I believe that to answer it well, your question will require
more time and effort than the average amount of time and effort
associated with this price. Here is a link to guidelines about pricing
your question, in the pricing guide:
https://answers.google.com/answers/pricing.html
Subject: Re: Simulate Mouse Move in C++
From: efn-ga on 11 Jun 2004 23:29 PDT
 
I'd suggest either setting the MOUSEEVENTF_ABSOLUTE flag in the second
and third messages, or setting dx and dy to zero in those messages. 
This is just a guess based on a quick search.  I haven't tested or
debugged it.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy