我如何可以模拟在屏幕上的某个位置点击鼠标? [英] How can I simulate a mouse click at a certain position on the screen?

查看:1878
本文介绍了我如何可以模拟在屏幕上的某个位置点击鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是操作鼠标。这将是对我自己的目的简单的宏。因此,将我的鼠标移动到某个位置在屏幕上,点击喜欢我与一定的时间间隔点击

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.

推荐答案

下面是一个code正在使用的非托管函数来模拟鼠标点击:

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

要保持pssed为特定的时间将鼠标$ P $您可以睡眠()正在执行这个功能,例如线程:

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

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

以上code将继续pssed 1秒钟鼠标$ P $除非用户presses的释放鼠标按钮。 同时,确保不会在主UI线程上执行该code,因为它会导致它挂

这篇关于我如何可以模拟在屏幕上的某个位置点击鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆