Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
there is a question bother me for a while. i want to simulate mouse movement by c#, and i use the codes below:
C#
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
       public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);


the simple code only works with in the application. how to make it work out of the form?

thx.
Posted

Also, don't use mouse_event. Even though it's bad to use P/Invoke (please see Solution 1), sometimes you still need to use it, for much more powerful effects. But the obsolete function mouse_event has been superseded with another function, SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

This is how to use it with P/Invoke: http://www.pinvoke.net/default.aspx/user32.sendinput[^].

Now you have all you need.

—SA
 
Share this answer
 
Comments
600ml 29-May-13 4:38am    
thx alex for ur quick feedback. i am the beginner of the coding, and hope u can give more guide here. what i am doing is:
1. have a configuration file with all the mouse actions it need to do
2. the application read the configuration file and follow the steps to simulate all the mouse actions.

so could guide me:
3. the Cursor class seems can't do the click action, so should i use the sendinput?
4. when the application begin, all the mouse actions need to be excused out of the application ( i mean the form of the application). how could it do?

appreciated.
Sergey Alexandrovich Kryukov 29-May-13 8:38am    
I re-numbered your items above. Now, let's see:

#1, #2: That will need a separate thread and follow the steps in it, using the approach shown in Solution 1 (not this Solution). The cursor should be set via the Control.Invoke.

#3: Yes, the click would require SendInput.
#4: In a separate thread.

Alternatively, you can use a timer and do motion in the timer events, but thread is easier.

You probably also need to have some time intervals in your file. If not, you will need to do all the actions in a separate thread with some uniform delay (Thread.Sleep). You can compare your timing with "Read time" using System.Diagnostics.Stopwatch.

Overall, all this work is not quite for a beginner. You should better feel comfortable enough.
—SA
Don't use P/Invoke without extreme need; it will break all your platform compatibility.

What you need to do with a Forms application is quite easy working purely in .NET FCL. Here is how:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx[^].

As simple as that.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900