Simulating mouse movements

B

bwahahahaha

How can I inject the MouseMove events to move the mouse programatically in
C#, or is this not possible and I have to drop to Win32 for this?
 
M

Michael Bray

How can I inject the MouseMove events to move the mouse
programatically in C#, or is this not possible and I have to drop to
Win32 for this?

use the static property Cursor.Position

-mbray
 
B

bwahahahaha

But thats per control right? I can only do this at the form level, but not
globally on the desktop?
 
M

Michael Bray

But thats per control right? I can only do this at the form level,
but not globally on the desktop?

Cursor.Position will move the mouse but not simulate clicks. If you want
to simulate a click on a control, such as 'Button1', and you have a click
handler, such as:

public void button1_Click(object sender, MouseEventArgs e)
{
...
}

then just call (from wherever you need to simulate the click):

button1_Click(null, null)

as long as you don't process sender or the MouseEventArgs. If you do
process them, then simulate them as well...

MouseEventArgs mea = new MouseEventArgs(...);
<set the properties of mea>
button1_Click(button1, mea);
 
B

bwahahahaha

SO there isnt an equivelent of PostMessage in C# to send events, we just
call the eventhandler directly? hmm
 
M

Michael Bray

But, what if the eventhandler method isnt directly available?

Well I guess the real question is, what are you really trying to do? Or,
why are you trying to simulate a mouse click?

IF you are just trying to perform the action that the application would
perform, then the method will work. You *could* make the event handler
public, but that's not exactly the best programming practice. Better would
be to put the code that runs in a separate public function, and have the
handler call that code.

IF you are actually trying to write a simulator (eg an automation package)
then I'll have to admit that I don't have the knowledge to answer your
question. I haven't personally noticed anything in .NET that will post a
windows message to another application, but there may be one. Otherwise,
P/Invoke isn't too difficult to get your arms around - give it a shot.

-mbray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top