[英]How to programmatically (C# or Java) launch an app in Windows and invoke click in it's window?
There is a simple application that works in Windows. 在Windows中有一个简单的应用程序。 It has very simple interface: squre window with buttons in fixed coordinates.
它具有非常简单的界面:带有固定坐标按钮的方形窗口。
I need to write a program that makes use of this application: to launch it and to click one of buttons (let's say invoke a click at (150,200)). 我需要编写一个使用此应用程序的程序:启动它并单击按钮之一(假设在(150,200)处单击)。
Is there any way to do it in Java or .NET? 用Java或.NET有什么方法吗?
The Java based solution is to launch the app. 基于Java的解决方案是启动应用程序。 in a
Process
and use the Robot
to interact with it. 在
Process
并使用Robot
与其进行交互。
The best solution on this thread was by @HFoE but deleted by a moderator. 此线程上的最佳解决方案是@HFoE,但被主持人删除。 For reference, it basically came down to..
供参考,基本上可以归结为..
If you want to control another Windows application, use a tool that was built specifically for this such as AutoIt V3 .
如果要控制另一个Windows应用程序,请使用为此专门构建的工具,例如AutoIt V3 。
Since "Don't do it" seems to be considered a valid answer when an alternative is supplied (by general opinion on Meta), I cannot understand why the answer was deleted. 由于“不这样做”似乎在提供替代选项时被视为有效答案(根据Meta的一般观点),我无法理解为什么删除了答案。
As Hovercraft Full Of Eels if you can - use autoit - it's much easier. 作为气垫船充满鳗鱼,如果可以的话,请使用autoit,这要容易得多。 If AutoIt is not an option then you will need to use winAPI functions in order to do it.
如果没有AutoIt选项,则需要使用winAPI函数。
For example to call mouseclick at coordinates: 例如,在坐标处调用mouseclick:
[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
}
But be aware, that to make click inside a window it needs to be at front of you - you cannot click to a minimized app for example. 但是请注意,要在窗口内单击必须位于您的前面-例如,您不能单击最小化的应用程序。
If you want to try - you can find all needed functions(how to run a programm, get needed window by hwnd and so on) at PInvoke 如果您想尝试-您可以在PInvoke找到所有所需的功能(如何运行程序,通过hwnd获取所需的窗口等)
For .Net you can pretty much use AutomationElement which I prefer. 对于.Net,您可以使用我更喜欢的AutomationElement 。 There's a bit of learning time, but it shouldn't take much.
有一些学习时间,但是不需要太多时间。 You can start your app with ProcessStartInfo .
您可以使用ProcessStartInfo启动您的应用程序。
If you have VS2010 Pro or Ultimate you can use the CodedUITests to generate a couple of button pushes. 如果您使用VS2010 Pro或Ultimate,则可以使用CodedUITests生成几次按钮按下。
As @Hovercraft Full Of Eels suggested - Autoit, Python could do the same 正如@Hovercraft Full Of Eels所建议-Autoit,Python可以做同样的事情
Yes - in C#... 是的-在C#中...
Process
class to start the process (there are plenty of resources on the web on how to do this. Process
类来启动该过程(网络上有很多有关如何执行此操作的资源。 However note that there are several things that can go wrong with this 但是请注意,这可能会导致一些错误
在.net中,您可以从System.Diagnostics进行Process.Start来启动应用程序,甚至可以传递参数,并且可以使用P / Invoke来模拟鼠标事件,因此在SO上已经有了答案
Here is my working test app to play with clicking in windows. 这是我的工作测试应用程序,可以在Windows中单击来玩。 We just start some app and hope to click it in right place) It would be nice to have some solution for capturing windows this way =)
我们只是启动了一个应用程序,希望在正确的位置单击它)如果能以这种方式获得捕获窗口的解决方案,那就太好了=)
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.