Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Community.

I'm new in this forum. I have already checked out several examples using Win32 API in order to simulate clicks and cursor movements. However I have few questions, hopefully somebody can answer them.

1) what's the difference between using win32 api and just using the .Net Cursor.position.

2) I need to build an tool that reads from an .xml file and depending on what it read it activates certain stuff in another program.Is there anyway to click on an specific item in the other program running? for example lets say I read from the .xml file switch 1 "on", can I somehow make the cursor go to switch one? without using x,y coordinates?

I just need to know if it is possible since I have no clue how to do move the cursor using the x,y coordinates.

thank you very much.
Posted
Comments
Philippe Mori 22-Aug-11 19:44pm    
It would be very hard to do it reliably since a lot of thing can make it not works as expected in some case... For example, some actions might depend on the foreground application and a windows can popup in some cases like when an application just finished downloading an upgrade and ank you if you want to install it...

1 solution

There is only one method of comprehensive simulation of mouse and keyboard input: P/Invoked raw Windows API SendInput, see http://msdn.microsoft.com/en-us/library/ms646304%28v=vs.85%29.aspx[^].

Note that there are legacy functions keybd_event and mouse_event, but they were superseded by SendInput which is recommended to use.

The simulation is done on lower level and does not depend on and Windows, Forms, etc. The simulation is done as the events came from real keyboard and mouse.

That should answer your general questions and the question #2. Now, about your question #1, "the difference". First, please understand that no question in the format "what's the difference between {0} and {1}?" can be logically correct. Try to answer such questions on the topics you're well familiar with and you will feel it. The property Cursor.Position is actually System.Windows.Forms.System.Windows.Forms.Cursor.Position, it is limited to System.Windows.Forms and to the function of reading and setting the cursor coordinates. If you need just that, it should be your preferred method. The function SendInput does a lot more; look by yourself.

From the other hand, using P/Invoke totally breaks your application platform compatibility. If you use this function, your code will only work on Windows. If you write pure .NET application, limit it to the use of standard CLR libraries (and a lot of non-standard ones, such as System.Windows.Forms) you will be able to run your application without recompilation on many platforms, such as Linux, Mac OS and more. CLR is standardized under EMCA and ISO standard and is implemented on different platforms. One most essential product lik this is Mono, see http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/[^]. You can use all of that without much effort from your side if you refrain from using Windows-specific P/Invoke. You decide.

—SA
 
Share this answer
 
Comments
nanomass 24-Aug-11 15:52pm    
Thank you for your answer I spent an entire day researching and now I feel way more comfortable with win32 API, it turns out there is a lot of info out there(google), definitely need to use win32 api for my tool.

thank you very much.
Sergey Alexandrovich Kryukov 12-Apr-12 12:05pm    
You are very welcome.
Good luck, call again.
--SA

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