模拟鼠标点击在C#中活动窗口的某个位置

模拟鼠标点击在C#中活动窗口的某个位置

问题描述:

下面是原来的问题,但它被认为是为Java:
模拟鼠标点击在爪哇活动窗口的某个位置?

Here is the original question but it is considered to java: Simulate mouse clicks at a certain position on inactive window in Java?

不管怎么说,我建立一个机器人在后台运行。这个机器人需要我去点击。当然,我希望能够在机器人运行的同时做其他的事情。

Anyways, I'm building a bot to run in the background. This bot requires me to click. Of course, I want to be able to do other things while the bot is running.

所以我在想,如果有可能,我以模拟在鼠标点击非活动窗口上的某个位置。

So I was wondering if it was possible for me to simulate a mouse click at a certain position on an inactive window.

如果这是可能的,我将不胜感激,如果你们能帮助我。

If this is possible, I would greatly appreciate it if any of you could help me.

感谢

是的,它是可能的,这里是我用以前的学校项目中的代码:

Yes it is possible, here's the code I used for a previous school project:

[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;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

//This simulates a left mouse click
public static void LeftMouseClick(Point position)
{
    Cursor.Position = position;
    mouse_event(MOUSEEVENTF_LEFTDOWN, position.X, position.Y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, position.X, position.Y, 0, 0);
}



编辑:看来,的 mouse_event 功能被替换的 SendInput() ,但它仍然有效(的Windows 7及更早版本)

EDIT : It seems that the mouse_event function was replaced by SendInput() but it still works (Windows 7 and earlier)