用C#控制另一个应用程序

我需要通过模拟鼠标移动和键盘输入来控制其他应用程序。 我如何在C#中实现这一目标? 它甚至可能吗?

你看过White TestStack了吗?

示例代码:

Application application = Application.Launch("foo.exe"); Window window = application.GetWindow("bar", InitializeOption.NoCache); Button button = window.Get 

我认为它不会比那更好。 该库由ThoughtWorks创建。

请参阅此页面上的“将击键发送到其他应用程序”:

http://msdn.microsoft.com/en-us/library/ms171548.aspx

使用SendMessage Native Win32 API。 Dll从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); 

你可以使用AutoIT ,它是免费软件,它有一个你可以导入C#(DllImport)的DLL版本。 它允许您在C#上的另一个应用程序上单击控件,编写字符串以编辑框,进行combobox选择等。

我试着这样做:使用我使用过这个类的鼠标

(您需要using System.Runtime.InteropServices;添加using System.Runtime.InteropServices;

 public class MouseClick1 //public is important here** { [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 int CoordX { get; set; } public int CoordY { get; set; } public void Click1() { Cursor.Position = new Point(CoordX, CoordY); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } } 

在你的form1之后(假设名字是form1)你可以

  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 } 

和我一起工作。