Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Mouse to Position and Left Click

I'm working on an Windows Form Application in C#, Framework 4 (32 bit).

I have a list that holds coords of the mouse, and I can capture them. So far so good.

But at some point, I want to go to those coords and left mouse click on it.

This is how it looks like right now:

for (int i = 0; i < coordsX.Count; i++)
{
    Cursor.Position = new Point(coordsX[i], coordsY[i]);
    Application.DoEvents();
    Clicking.SendClick();
}

And the Clicking class:

class Clicking
    {
        private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
        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)
        public static void SendClick()
        {
            // Cursor.Position = location;
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
        }
    }

But I'm getting this error:

Could not load type 'program.Clicking' from assembly 'program, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'mouse_event' has no implementation (no RVA).

And i realy don't understand what the problem is... Do you guys know what the problem is? or do you know an better way to do what i'm trying to do?

like image 409
Mathlight Avatar asked Nov 22 '12 22:11

Mathlight


People also ask

How do you left click without a mouse?

You can do a left click by pressing the forward slash key (/), followed by the 5 key.


2 Answers

Have you included the following line?

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
   UIntPtr dwExtraInfo);

This will import the function mouse_event from the user32 dll, which is what you are trying to use within your program. Currently your program does not know about this method within the DLL untill you specify wher it comes from.

The website PInvoke.net user32 Mouse Event is quite handy for the basics on this sort of thing.

The answer to Directing mouse events [DllImport(“user32.dll”)] click, double click will be of great help to your understanding as well.

The flags are what commands you want to send into the mouse_input function, in that example you can see that he is sending both mouse down and mouse up in the same line, this is fine because the mouse_event function will split those flags up and execute them consecutively.


Also note that this method has been superseded by the SendInput command, a good example of SendInput and SetMousePos can be found At this Blog

like image 192
Serdalis Avatar answered Oct 06 '22 09:10

Serdalis


I guess you are missing the following line

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
like image 2
Afnan Bashir Avatar answered Oct 06 '22 09:10

Afnan Bashir