有一个简单的应用程序在Windows中工作。 它具有非常简单的界面:带有固定坐标的button的方形窗口。
我需要编写一个使用这个应用程序的程序:启动它并单击其中一个button(假设在(150,200)处调用点击)。
有没有办法在Java或.NET中做到这一点?
基于Java的解决方案是启动应用程序。 在一个Process
并使用Robot
与它进行交互。
这个线程的最佳解决方案是@HFoE,但由主持人删除。 作为参考,它基本上归结为..
如果您想要控制另一个Windows应用程序,请使用专为此构建的工具,例如AutoIt V3 。
由于“不这样做”似乎被认为是一个有效的答案,当提供一个替代方案时(通过Meta的一般意见),我不明白为什么答案被删除。
作为气垫船充满了鳗鱼,如果你可以 – 使用自动 – 这是更容易。 如果AutoIt不是一个选项,那么您将需要使用winAPI函数来执行此操作。
例如要调用鼠标点击坐标:
[DllImport("user32.dll")] static extern bool SetCursorPos(int x, int y); [DllImport("user32.dll")] static extern bool GetCursorPos(ref Point lpPoint); [DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); public void LeftMouseClick(int xpos, int ypos) //Make a click at specified coords and return mouse back { Point retPoint = new Point(); GetCursorPos(ref retPoint); // set retPoint as mouse current coords SetCursorPos(xpos, ypos); //set mouse cursor position mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); //click made SetCursorPos(retPoint.X, retPoint.Y); //return mouse position to coords }
但是请注意,要在窗口内点击它需要在你的前面 – 例如,你不能点击最小化的应用程序。
如果你想尝试 – 你可以在PInvoke找到所有需要的函数(如何运行一个程序,通过hwnd获得所需的窗口等)
对于.Net,你可以使用我更喜欢的AutomationElement 。 有一些学习时间,但不应该花太多时间。 您可以使用ProcessStartInfo启动您的应用程序。
如果您拥有VS2010 Pro或Ultimate,则可以使用CodedUITests生成一些按钮按钮。
正如@Hovercraft Full Of Eels建议的 – 自动,Python可以做同样的事情
是的 – 在C#中
Process
类来启动这个过程(网上有很多关于如何做到这一点的资源。 但请注意,有几件事情可能会出错
在.net中,你可以从System.Diagnostics的Process.Start启动一个应用程序,甚至可以传递参数,并模拟鼠标事件,你可以使用P / Invoke已经有一个答案在这里
这是我的工作测试应用程序来玩点击窗口。 我们只是启动一些应用程序,并希望在正确的地方点击它)这将是很好的有一些解决方案来捕获窗口这样=)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; namespace ConsoleApplication8 { class Program { static void Main(string[] args) { var startInfo = new ProcessStartInfo(@"C:\Users\Bodia\Documents\visual studio 2010\Projects\ConsoleApplication8\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe"); startInfo.WindowStyle = ProcessWindowStyle.Maximized; Console.WriteLine(1); var process = Process.Start(startInfo); Console.WriteLine(2); Thread.Sleep(400); Console.WriteLine(3); LeftMouseClick(1000, 200); Console.WriteLine(4); } static void CursorFun() { Point cursorPos = new Point(); GetCursorPos(ref cursorPos); cursorPos.X += 100; Thread.Sleep(1000); SetCursorPos(cursorPos.X, cursorPos.Y); cursorPos.X += 100; Thread.Sleep(1000); SetCursorPos(cursorPos.X, cursorPos.Y); cursorPos.X += 100; Thread.Sleep(1000); SetCursorPos(cursorPos.X, cursorPos.Y); cursorPos.X += 100; Thread.Sleep(1000); SetCursorPos(cursorPos.X, cursorPos.Y); } [DllImport("user32.dll")] static extern bool SetCursorPos(int x, int y); [DllImport("user32.dll")] static extern bool GetCursorPos(ref Point lpPoint); [DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); public static void LeftMouseClick(int xpos, int ypos) //Make a click at specified coords and return mouse back { Point retPoint = new Point(); GetCursorPos(ref retPoint); // set retPoint as mouse current coords SetCursorPos(xpos, ypos); //set mouse cursor position mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); //click made SetCursorPos(retPoint.X, retPoint.Y); //return mouse position to coords } struct Point { public int X; public int Y; } private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; private const int MOUSEEVENTF_RIGHTDOWN = 0x08; private const int MOUSEEVENTF_RIGHTUP = 0x10; } }