C# 使用C控制另一个应用程序#
我需要通过模拟鼠标移动和键盘输入来控制其他应用程序。我如何在C#中实现这一点?有可能吗 请参阅本页的“向其他应用程序发送击键”: 你看过吗 示例代码:C# 使用C控制另一个应用程序#,c#,winforms,C#,Winforms,我需要通过模拟鼠标移动和键盘输入来控制其他应用程序。我如何在C#中实现这一点?有可能吗 请参阅本页的“向其他应用程序发送击键”: 你看过吗 示例代码: Application application = Application.Launch("foo.exe"); Window window = application.GetWindow("bar", InitializeOption.NoCache); Button button = window.Get<Button>("sa
Application application = Application.Launch("foo.exe");
Window window = application.GetWindow("bar", InitializeOption.NoCache);
Button button = window.Get<Button>("save");
button.Click();
Application=Application.Launch(“foo.exe”);
Window Window=application.GetWindow(“bar”,InitializeOption.NoCache);
按钮=窗口。获取(“保存”);
按钮。单击();
我认为没有比这更好的了。该库是由ThoughtWorks创建的 使用本机Win32 API。此方法来自User32.dll。您可以使用此API发送键盘和鼠标消息您可以使用p/invoke,我窃取了以下代码,用于在具有已知手柄的按钮上随机单击鼠标:
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
[StructLayout(LayoutKind.Sequential)]
public struct Rectangle
{
public int X;
public int Y;
public int Width;
public int Height;
}
private static void Click(IntPtr Handle)
{
lock (typeof(MouseAction))
{
Rectangle buttonDesign;
GetWindowRect(Handle, out buttonDesign);
Random r = new Random();
int curX = 10 + buttonDesign.X + r.Next(100 - 20);
int curY = 10 + buttonDesign.Y + r.Next(60 - 20);
SetCursorPos(curX, curY);
//Mouse Right Down and Mouse Right Up
mouse_event((uint)MouseEventFlags.LEFTDOWN, curX, curY, 0, 0);
mouse_event((uint)MouseEventFlags.LEFTUP, curX, curY, 0, 0);
}
}
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
private static extern void mouse_event(
long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out Rectangle rect);
你可以使用,这是免费软件,它有一个dll版本,你可以导入到C#(DllImport)。它允许您在C#的另一个应用程序上单击控件、编写字符串以编辑框、选择组合框等。我也尝试过这样做:使用鼠标我使用了这个类 (您需要使用System.Runtime.InteropServices;添加
)
在你的form1上(假设名字是form1)之后,你可以
[……]
}
}
要获得指针上的坐标,我使用计时器和标签:
private void timer1_Tick(object sender, EventArgs e)
{
Coord.Text = Cursor.Position.X.ToString() + " : " + Cursor.Position.Y.ToString(); //Premet d'avoir les coord en direct
}
和我一起工作吧。也许乔恩·斯基特是唯一一个能做到这一点的人:)看起来像一个复制品。我发现,通过搜索这个问题的公认答案与副本的公认答案不同(两者都提供了合理的解决方案)。我试图浏览White project的文档,但它开始给出错误!顺便说一句,“白色”将为您提供OO方法来控制基于Windows的应用程序。我正在尝试访问Spotify(一个音乐程序)并单击“播放”按钮,但它似乎完全是图形化的,因为我找不到spy++的任何句柄或任何东西。我该怎么做?@PontusMagnusson你是怎么做到的?我和你有同样的问题,但不幸的是我没有代码了。我相信我将windows事件排队以控制应用程序的不同元素。今天我想我应该在你的代码中使用Spotify API代码>应该是按钮2。单击1()代码>。
public partial class form1 : Form
{
MouseClick1 button1 = new MouseClick1();
MouseClick1 button2 = new MouseClick1();
public void Simulate_button1and2_Click(object sender, EventArgs e)
{
button1.CoordX = 1652; //random coordinates
button1.CoordY = 225;
button2.CoordX = 1650;
button2.CoordY = 250;
button1.Click1();
button1.Click2();
private void timer1_Tick(object sender, EventArgs e)
{
Coord.Text = Cursor.Position.X.ToString() + " : " + Cursor.Position.Y.ToString(); //Premet d'avoir les coord en direct
}