locked
Mouse movement in C#.

    Question

  • How exactly is mouse movement achieved in C#?  I know that in VB.net, it can be implemented using the gdi32 MouseEvent library, but what I truly need to know is how to achieve it in C# (along with simulated clicks, for macros).
    Sunday, July 02, 2006 12:36 AM

Answers

  • how do you mean mouse movement?

    do you mean for example the mouse moving from the current position to say, the left hand side of the screen?

    if this is what you mean, try this:

     



    Cursor.Position = new Point(Cursor.Position.X + 10, Cursor.Position.Y + 10);

     

     

    this will move the mouse from the current position, either way, and add 10 to the co-ordinates

    Mouse is known as a Cursor when coming to develop with it.

    To redraw or relocate the mouse, you simply create a new point and the system will redraw the mouse (move it) to the new location specified

    Sunday, July 02, 2006 2:22 AM

All replies

  • how do you mean mouse movement?

    do you mean for example the mouse moving from the current position to say, the left hand side of the screen?

    if this is what you mean, try this:

     



    Cursor.Position = new Point(Cursor.Position.X + 10, Cursor.Position.Y + 10);

     

     

    this will move the mouse from the current position, either way, and add 10 to the co-ordinates

    Mouse is known as a Cursor when coming to develop with it.

    To redraw or relocate the mouse, you simply create a new point and the system will redraw the mouse (move it) to the new location specified

    Sunday, July 02, 2006 2:22 AM
  • I mean like...Setting the cursor position, and then simulating mouse-clicks.
    Sunday, July 02, 2006 2:24 AM
  • well the code example I gave handles the setting cursor position :-)

    As for simulating mouse clicks, there are various events for the mouse (MouseClick, MouseMove etc...) which you can implement and simulate with.

     

    If you are wanting to simulate clicks in your application, you can use the Control.PerformClick, which will perform a click on that control.

    You may also PInvoke to simulate clicks if you wish however lets try to stick to the .NET way...

     

    You can try to do the following:

    create a mouseclick event in your application (main form)

    Call the event and send it mouseEventArgs, within the mouseeventargs, you can specify the mousebutton click to use (left/right), the position of the mouse and the number of times to click.

     

    Have a look at this resource about simulate mouse/keyboard events

    http://msdn2.microsoft.com/en-us/library/ms171548.aspx

     

    I am unsure if the express edition allows the usage of PInvoke/dll call but if so try using PInvoke:

     


    private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
    [
    DllImport("user32.dll")]
    private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);

    ..
    ..

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

     

     

     

    Make sure you use the System.Runtime namespace

    Sunday, July 02, 2006 2:29 AM
  • All right, thanks for all of your help!
    Sunday, July 02, 2006 2:30 AM
  • if you want to scrol to the cursor position just use this, its so simple

    textBox1.ScrollToCaret();
    

     

    Tuesday, September 13, 2011 9:19 AM