Jump to content

  • Log In with Google      Sign In   
  • Create Account

[.net] How to simulate a mouse click in C#?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 kaban   Members   -  Reputation: 122

Like
0Likes
Like

Posted 22 May 2005 - 08:16 AM

I have been searching on google and msdn trying to figure this out and got really confused. What I am trying to do is make a windows application that simulates mouse clicks after I hit a certain button. I want the program to make the mouse click anywhere and continue to click even if the program is minimized, until I hit the button that tells it to stop. I have the C++ code for it, which is very simple: //Simulate left mouse click mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0); Is there any equivalent C# code? Is there a way to port this C++ code into my C# program? I am completely new to .NET, sorry for the noob questions! Thanks

Sponsor:

#2 DaWanderer   Members   -  Reputation: 538

Like
0Likes
Like

Posted 22 May 2005 - 08:31 AM

Yes, you can import this function into your C# program. Here's an example:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class Form1 : Form
{
[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);

private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

public Form1()
{
}

public void DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}

//...other code needed for the application
}



#3 kaban   Members   -  Reputation: 122

Like
0Likes
Like

Posted 22 May 2005 - 04:16 PM

Awesome! That worked perfectly. Thanks a lot. :D

#4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 07 October 2006 - 04:08 PM

What about right clicks?

#5 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 07 October 2006 - 11:23 PM

replace left with right in the call to mouse_event() maybe ?

#6 BradSnobar   Members   -  Reputation: 232

Like
0Likes
Like

Posted 08 October 2006 - 07:22 AM

Be careful with an application like this.
I have written some GUI test harnesses that had functionality like this.
It can be difficult to make them work in a way that handles a lot of situations gracefully.

Here are some notes of interest if you plan on expanding your tool.
screen resolution changes, time between keypresses and click messages, lost messages, etc.

A good help is if there is a hook that you can use to check if the keypress, or click was successful.



#7 drexlin   Members   -  Reputation: 122

Like
0Likes
Like

Posted 14 June 2007 - 10:32 AM

I'm trying to use this class, but no matter what values I use for X and Y, the simulated click always happens wherever the mouse cursor is.

What is the point of the X and Y if not to tell the function the location of the mouse click

#8 GroZZleR   Members   -  Reputation: 820

Like
0Likes
Like

Posted 14 June 2007 - 04:57 PM

Quote:
Original post by drexlin
I'm trying to use this class, but no matter what values I use for X and Y, the simulated click always happens wherever the mouse cursor is.

What is the point of the X and Y if not to tell the function the location of the mouse click



//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;


They're setting the X and Y to the current mouse position. Set the values to whatever you want.



#9 drexlin   Members   -  Reputation: 122

Like
0Likes
Like

Posted 15 June 2007 - 02:29 AM

Quote:
Original post by GroZZleR
Quote:
Original post by drexlin
I'm trying to use this class, but no matter what values I use for X and Y, the simulated click always happens wherever the mouse cursor is.

What is the point of the X and Y if not to tell the function the location of the mouse click



//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;


They're setting the X and Y to the current mouse position. Set the values to whatever you want.


I did change these values. It still doesn't work.

#10 Americanadian   Members   -  Reputation: 122

Like
0Likes
Like

Posted 20 June 2008 - 05:21 AM

I'm having the exact same problem. I can't figure out how to set the coordinates for x and y; no matter what values I use, the mouse stays in the same position, I used the right button down and up to test this...

#11 Americanadian   Members   -  Reputation: 122

Like
0Likes
Like

Posted 20 June 2008 - 05:31 AM

Hey Drexlin, I figured it out.

Add a reference to System.Drawing to your .cs and then when you solve for your x and y coords, make your next line as follows:

Cursor.Position = new Point((int)x, (int)y);

Then use it in your SendMouseInput or mouse_event function (Cursor.Position.X and Y) instead of your original x and y.

Cheers, hope that helps you out too!

Chris

#12 cecenzor   Members   -  Reputation: 100

Like
0Likes
Like

Posted 04 October 2009 - 10:19 PM

hi... i appreciate that if anyone can help me...
I have been trying to disable mouse click for startup button in c#...

I tried several ways but i couldnt make it...

please help me....


#13 Codeka   Members   -  Reputation: 1157

Like
0Likes
Like

Posted 04 October 2009 - 10:45 PM

If you have a new question, you should start a new thread. Also, if you're going to start a new thread, I would suggest you also provide a reason for why you want to "disable mouse click for [start] button". If you're writing a kiosk program, there's better ways to do it than messing with mouse messages, but if you don't tell us we can't really guess.

#14 samar_for_islam   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 February 2010 - 11:44 PM

it works Correctly.. thanks very much




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS