Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mouse_event() in C#? How do I simulate a mouse click?

620 views
Skip to first unread message

optiplex

unread,
Apr 7, 2002, 6:30:41 AM4/7/02
to
How do I simulate a mouse click in C#? I believe that this could be
done in C++ with mouse_event(). Thanks in advance.

Eliyahu (Vyacheslav) Biktagirov

unread,
Apr 7, 2002, 6:50:52 AM4/7/02
to
Use SendMessage or PostMessage API

Yan-Hong Huang(MS)

unread,
Apr 8, 2002, 2:35:05 AM4/8/02
to
Try PerformClick to see if it is what you want.

If not, try PInvoke to call Win32 API SendMessage.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com

optiplex

unread,
Apr 8, 2002, 7:39:43 PM4/8/02
to
yhhuan...@microsoft.com (Yan-Hong Huang(MS)) wrote in message news:<uuKZgds3BHA.1600@cpmsftngxa08>...

Could you please provide me an example or link me to one? I am a
relatively newbie, so this is new to me.

Thank you.

Jason Sacks

unread,
Apr 8, 2002, 9:26:23 PM4/8/02
to
If you have yave a button, MenuItem or Radio button the do the
following:

//myControl must be a Button, MenuItem or RadioButton or a class that
inherites from one of these

myControl.PerformClick();


If you want to use it on another object that responds to a click then
you can use the following class. This does not pay attention to whether
or not you are clicking the control or alt buttons. It also does not
allow you to specify the location that you are clicking on. This just
sends a click message to the control. It is basic but it should help you
along and give you an idea.

public sealed class MessageSender
{
private const UInt32 WM_LBUTTONDOWN = 0x201;
private const UInt32 WM_LBUTTONUP = 0x202;


[DllImport("user32.dll")]
private static extern int SendMessage( IntPtr handle, UInt32 message,
int wParam,
int lParam);


public static void SendClick(Control receiver)
{
if (receiver != null)
{
SendMessage(receiver.Handle,WM_LBUTTONDOWN,0,0);
SendMessage(receiver.Handle,WM_LBUTTONUP,0,0);

}
}
}


Regards,

Jason Sacks
csharpe...@attbi.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

optiplex

unread,
Apr 9, 2002, 4:40:46 AM4/9/02
to
Thank you for your timely response. However, it is not a Windows
control that I would like to click. Instead, I am trying to implement
something similiar to GhostMouse (basically a mouse macro recorder -
record your mouse movements and clicking, then replays it). All I have
left to do is have the mouse pointer click programmatically, and it
does not necessarily have to be on a Windows control (radiobox,
button, etc). Do you, or anyone else know how to do such a thing?

Thank you.


Jason Sacks <csharpe...@attbi.com> wrote in message news:<#vslQW23BHA.2208@tkmsftngp05>...

optiplex

unread,
Apr 9, 2002, 4:45:23 AM4/9/02
to
Also, a problem I am having is that whenever I try to do a DLLImport,
I get the following error message when I compile:

"The type or namespace name 'DllImport' could not be found (are you
missing a using directive or an assembly reference?)"

Is there something that I am forgetting?

Thanks in advance.

Jason Sacks <csharpe...@attbi.com> wrote in message news:<#vslQW23BHA.2208@tkmsftngp05>...

Steve Brecher

unread,
Apr 9, 2002, 12:25:26 PM4/9/02
to
"optiplex" <optip...@gotmail.com> wrote:
> Also, a problem I am having is that whenever I try to do a DLLImport,
> I get the following error message when I compile:
>
> "The type or namespace name 'DllImport' could not be found (are you
> missing a using directive or an assembly reference?)"
>
> Is there something that I am forgetting?

using System.Runtime.InteropServices;

--
For mail, please use my surname where indicated:
st...@surname.reno.nv.us (Steve Brecher)

optiplex

unread,
Apr 9, 2002, 5:07:37 PM4/9/02
to
How will I be able to call mouse_event from c#? Can you please provide an example?

"Steve Brecher" <Steve_...@see.sig.at.end> wrote in message news:<eBuA7L#3BHA.2144@tkmsftngp07>...

Jason Sacks

unread,
Apr 9, 2002, 9:22:20 PM4/9/02
to
In order to do what you are talking about you are going to have to
create a windows hook. Investigate SetWindowsHookEx on msdn. Here is a
link. Lookout for the wrap around. If you have problems with the link
then search for SetWindowsHookEx on http://msdn.microsoft.com

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
/platform/CommCtls/winui/hooks_7vaw.asp


This topic pretty in depth and it would take quite a while for me to
write how to do this in C#. Basically you create a hook into the windows
messages. You record the mouse messages (location and button). Then you
can play it back with the .

You may want to do a WH_JOURNALRECORD and a WH_JOURNALPLAYBACK. I would
help more but I have a lot of work to do on my own :-P

Sascha Sertel

unread,
May 1, 2002, 11:43:25 AM5/1/02
to
Jason,

I tried your solution with the DLLImport and SendMessage API, but I
still have some problems:

- As you said yourself I can only use this solution together with an
object. What I would like to have is a solution that just sends a Mouse
Click event from the location where the mouse is at that moment,
especially when outside the program window.
- I would also like to implement clicking for other buttons, such as the
right or middle mouse button. I found numerous event names for this,
i.e. WM_RBUTTONDOWN, but I couldn't find the hex numbers you were using
for the click events you were giving in your sample code. Where can I
find the other hex values?

Thanks and greetings,
Sascha

Jason Sacks

unread,
May 2, 2002, 9:31:11 PM5/2/02
to
Look in the file named
Program Files\Microsoft Visual Studio
NET\Common7\Tools\Bin\Win32API.Txt

Regards,

Jason Sacks
csharpe...@attbi.com

Sascha Sertel

unread,
May 6, 2002, 12:52:02 PM5/6/02
to
I got a little further with the problem, I found out how to specify a
mouse position for the sendmessage command. In case anyone is
interested, it looks like this:

public static void SendClick(Control receiver, Point location)
{
if ( receiver != null )
{
SendMessage(receiver.Handle, WM_LBUTTONDOWN, 0, (location.Y *
0x10000) + location.X);
SendMessage(receiver.Handle, WM_LBUTTONUP, 0, (location.Y *
0x10000) + location.X);
}
}


But there is still a problem:
When I send a mouseclick to a window with mouse coordinates, it is still
only the specified window which gets clicked. If there is a button at
the specified location, it doesn't get clicked but the window underneath
it. To make it click that button you have to find out which object is
currently at the mouse position. I tried to use the windowfrompoint api
command, but I got strange exceptions.

Any solutions or hints?

thanks,
Sascha

Sascha Sertel

unread,
May 6, 2002, 3:52:07 PM5/6/02
to
After days and hours of reading through articles and stuff, I tried the
most simple solution and it worked exactly as I wanted it from the
beginning:


private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

[DllImport("user32.dll")]
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);

public static void SendClick(Point location)
{
Cursor.Position = location;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}


Why did none of you guys suggest this solution? Is there any sideeffect
or disadvantage I should know about? But this was the only solution so
far that did exactly what I wanted: Click at any given location and
execute the click event on any object the mouse might be over then,
especially if outside the windows form.

Thanks for your help anyways,

0 new messages