By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,910 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,910 IT Pros & Developers. It's quick & easy.

Simulating mouse movements

P: n/a
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?
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in
news:Od**************@TK2MSFTNGP12.phx.gbl:
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
Nov 15 '05 #2

P: n/a
But thats per control right? I can only do this at the form level, but not
globally on the desktop?
"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message
news:Xn*******************************@207.46.248. 16...
"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in
news:Od**************@TK2MSFTNGP12.phx.gbl:
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

Nov 15 '05 #3

P: n/a
"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in
news:OV*************@TK2MSFTNGP10.phx.gbl:
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);
Nov 15 '05 #4

P: n/a
SO there isnt an equivelent of PostMessage in C# to send events, we just
call the eventhandler directly? hmm
"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message
news:Xn*******************************@207.46.248. 16...
"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in
news:OV*************@TK2MSFTNGP10.phx.gbl:
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);

Nov 15 '05 #5

P: n/a
But, what if the eventhandler method isnt directly available?
"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message
news:Xn*******************************@207.46.248. 16...
"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in
news:OV*************@TK2MSFTNGP10.phx.gbl:
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);

Nov 15 '05 #6

P: n/a
"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in
news:#a**************@tk2msftngp13.phx.gbl:
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
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.