模拟鼠标点击在C#中活动窗口的某个位置 [英] Simulate mouse clicks at a certain position on inactive window in C#

查看:2082
本文介绍了模拟鼠标点击在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)

这篇关于模拟鼠标点击在C#中活动窗口的某个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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