我想制作一个鼠标宏。这既可以模拟鼠标事件,也可以使用我的电脑自己的屏幕光标。
宏应通过在 IDE 中键入方法来创建。然后,这些方法将在某个 .exe 的窗口上执行鼠标事件。通过使用坐标。
例如,这是我的目标,即在某个 .exe 窗口上执行模拟或非模拟鼠标左键单击的方法:
伪代码:
//Following method left clicks with the offset (x, y)
//from the windows top left corner. If the bool isSimulated
//is set to true the click will be simulated else the computers
//own mouse cursor will be moved and execute the mouse event.
LeftMouseClickOnWindow(x, y, isSimulated);
为了进一步说明问题,模拟鼠标点击 应该在窗口最小化或未聚焦时起作用。
我想知道创建这种实用程序的最佳方法是什么。
user32.dll 的函数是一个好的方法吗?
用 C++ 比用 C# 更容易吗?
非常感谢任何建议、来源、示例代码和评论!
最佳答案
C++ 和 C# 都很棒。 AutoHotKey可以胜任这份工作,但我和你一样——我喜欢写自己的东西。另一个选项是 AutoIt ,并且您可以在您的 C# 项目中使用它的 dll
...但是您必须确保它安装在每个系统上...这不是我经常遇到的奢侈。
这里有一些可以玩的东西。希望它能让您继续……请注意,它是 C#。在运行此代码之前,请确保您没有打开任何重要的鼠标所在的位置...这将沿对角线向右下方移动 20 次,并在每次移动时执行一次单击。你不希望它意外地关闭你的东西。因此,在运行之前将其全部最小化。
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ConsoleApplication
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
//private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
//private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void DoMouseStuff()
{
Cursor.Current = new Cursor(Cursor.Current.Handle);
var point = new Point(Cursor.Position.X, Cursor.Position.Y);
for (int i = 0; i < 20; i++, point.X += 10, point.Y += 10)
{
Cursor.Position = point;
Program.mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 0, 0);
System.Threading.Thread.Sleep(100);
}
}
static void Main(string[] args)
{
var prog = new Program();
prog.DoMouseStuff();
}
}
}
您需要为 System.Windows.Forms
和 System.Drawing
设置引用,如果您还没有设置的话。我将其作为控制台应用程序制作,因此需要为我进行设置。正如您所注意到的,我包含了 System.Threading.Thread.Sleep(100);
... 这样您就可以看到发生了什么。所以,我基本上是在放慢整个过程。它会移动并在每次移动时发出咔哒声(大约每 100 毫秒一次)。
熟悉 Cursor
和 user32.dll
。
最后但同样重要的是,这里是关于鼠标和键盘模拟的 MSDN 文档:http://msdn.microsoft.com/en-us/library/ms171548.aspx
关于c# - .exe 窗口中的鼠标模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281102/