如何在屏幕上的某个位置模拟鼠标点击?

时间:2023-02-15 21:14:56

What I want to do is to manipulate the mouse. It will be a simple macro for my own purposes. So it will move my mouse to certain position on the screen and click like I am clicking with certain interval.

我要做的是操作鼠标。这将是一个简单的宏以供我使用。它会将鼠标移动到屏幕上的某个位置,然后像我点击某个间隔一样点击。

2 个解决方案

#1


44  

Here's a code that is using unmanaged functions to simulate mouse clicks :

下面是一个使用非托管函数模拟鼠标点击的代码:

//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;

//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
    SetCursorPos(xpos, ypos);
    mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}

To keep the mouse pressed for a specific duration you can Sleep() the thread that is executing this function, for example :

要在特定的时间内按住鼠标,可以Sleep()执行此函数的线程,例如:

mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);

The above code will keep the mouse pressed for 1 second unless the user presses the releases the mouse button. Also, make sure to not execute this code on the main UI thread as it will cause it to hang.

以上代码将使鼠标保持1秒,除非用户按下鼠标按钮。另外,请确保不要在主UI线程上执行此代码,因为它会导致挂起。

#2


7  

You can move by XY position. Example below:

你可以移动XY的位置。在下面的例子:

windows.Forms.Cursor.Position = New System.Drawing.Point(Button1.Location.X + Me.Location.X + 50, Button1.Location.Y + Me.Location.Y + 30)

To click, you can use the below code:

要点击,您可以使用以下代码:

using System.Runtime.InteropServices;

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,             uint dwExtraInf);
private void btnSet_Click(object sender, EventArgs e)
    {
        int x = Convert.ToInt16(txtX.Text);//set x position 
        int y = Convert.ToInt16(txtY.Text);//set y position 
        Cursor.Position = new Point(x, y);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//make left button down
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//make left button up
    }

Credit to JOHNYKUTTY

信贷JOHNYKUTTY

#1


44  

Here's a code that is using unmanaged functions to simulate mouse clicks :

下面是一个使用非托管函数模拟鼠标点击的代码:

//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;

//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
    SetCursorPos(xpos, ypos);
    mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}

To keep the mouse pressed for a specific duration you can Sleep() the thread that is executing this function, for example :

要在特定的时间内按住鼠标,可以Sleep()执行此函数的线程,例如:

mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);

The above code will keep the mouse pressed for 1 second unless the user presses the releases the mouse button. Also, make sure to not execute this code on the main UI thread as it will cause it to hang.

以上代码将使鼠标保持1秒,除非用户按下鼠标按钮。另外,请确保不要在主UI线程上执行此代码,因为它会导致挂起。

#2


7  

You can move by XY position. Example below:

你可以移动XY的位置。在下面的例子:

windows.Forms.Cursor.Position = New System.Drawing.Point(Button1.Location.X + Me.Location.X + 50, Button1.Location.Y + Me.Location.Y + 30)

To click, you can use the below code:

要点击,您可以使用以下代码:

using System.Runtime.InteropServices;

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,             uint dwExtraInf);
private void btnSet_Click(object sender, EventArgs e)
    {
        int x = Convert.ToInt16(txtX.Text);//set x position 
        int y = Convert.ToInt16(txtY.Text);//set y position 
        Cursor.Position = new Point(x, y);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//make left button down
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//make left button up
    }

Credit to JOHNYKUTTY

信贷JOHNYKUTTY