I have been trying to find a way to make a mouse click programmatically but the results I find are quit strainous based on my level. I know how to position the mouse and everything but the click. I also know that there is a way to simulate a keyboard press with key events. So this got me to wonder, is there a way to make the m开发者_JS百科ouse click by pressing a keyboard key? I want to do this because I'm working on a educational project that shows beginners how to do simple functions on the computer, like how to create a file or open certain programs, so I need the mouse click to work based on the screen and outside of my app. Is this possible? all help will be appreciated.
you can simulate Mouse Click though following code
using System.Runtime.InteropServices;
[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 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);
}
mouse_event actually perform the moouse click.
This is not a c# answer but if you are trying to only automate stuff for teaching, this might help. You can capture a video of all your actions and highlight mouse clicks. A good software for that is the Camtasia studio. It is a paid software but there are other free screen recording software out there.
If you really want to just automate actual mouse clicks, try using Autoit http://www.autoitscript.com/site/autoit/
for animations. You can script anything from keyboard keypresses to mouse clicks in any area of the screen.
In case you want to try it out, to click on any position, all you would do is
MouseClick("primary", x-coordinate, y-coordinate, number-of-clicks)
// number of clicks = 2 for double click
$pos = MouseGetPos()
$pos[0] // contains the x coordinate of current mouse pos
$pos[1] // contains the y coord of current position
精彩评论