Simulate Mouse movement in C#

Hi all,

I am making  a Windows Form application in C#.
I want to be able to "simulate" the mouse movement and clicking. i.e. when I press the "start" button the move moves to a point specified (lets say move to 200,400). Then I want to "press" the left click.

Basically I am automating something, and I need this application to take the mouse to a certain position on the screen and then click there.

Any advice would be appreciated.

I have figured the code of how to move the mouse, from:
http://www.dotnet247.com/247reference/msgs/11/59571.aspx 

But I dont know how to do the click and rightclick thingi

Thanks.

dosdemonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cambo1982Commented:
Just for general information... the above VB Code converted to C#

-------------
using System.Runtime.interopservices

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
// the rest of the const val are in the other link

[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());
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dosdemonAuthor Commented:
Great! works perfect! Thanks
0
Tom KnowltonWeb developerCommented:
There is no "pure .NET" way to do this?  Must use API?
0
sapbucketCommented:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EventManager
{
      /// <summary>
      /// Summary description for MouseSimulator.
      /// </summary>
      public class MouseSimulator
      {
            public MouseSimulator()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
            private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
            private const UInt32 MOUSEEVENTF_MIDDLEDOWN = 0x0020;
            private const UInt32 MOUSEEVENTF_MIDDLEUP = 0x0040;
            private const UInt32 MOUSEEVENTF_MOVE = 0x0001;
            private const UInt32 MOUSEEVENTF_ABSOLUTE = 0x8000;
            private const UInt32 MOUSEEVENTF_RIGHTDOWN = 0x0008;
            private const UInt32 MOUSEEVENTF_RIGHTUP = 0x0010;

            [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 IntPtr());
                  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new IntPtr());
            }

      }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.