【发布时间】:2012-07-30 07:50:19
【问题描述】:
有一个可以在 Windows 中运行的简单应用程序。它的界面非常简单:带有固定坐标按钮的方形窗口。
我需要编写一个使用此应用程序的程序:启动它并单击其中一个按钮(假设在 (150,200) 处调用单击)。
有什么方法可以在 Java 或 .NET 中实现吗?
【问题讨论】:
标签: c# java windows user-interface click
有一个可以在 Windows 中运行的简单应用程序。它的界面非常简单:带有固定坐标按钮的方形窗口。
我需要编写一个使用此应用程序的程序:启动它并单击其中一个按钮(假设在 (150,200) 处调用单击)。
有什么方法可以在 Java 或 .NET 中实现吗?
【问题讨论】:
标签: c# java windows user-interface click
对于.Net,您几乎可以使用我更喜欢的AutomationElement。有一点学习时间,但应该不会花太多时间。您可以使用ProcessStartInfo 启动您的应用程序。
如果您有 VS2010 Pro 或 Ultimate,您可以使用 CodedUITests 生成几个按钮。
正如@Hovercraft Full Of Eels 所建议的那样——Autoit,Python 也可以这样做
【讨论】:
是的 - 在 C# 中...
Process 类启动进程(网上有很多关于如何执行此操作的资源。但是请注意,这可能会出现一些问题
【讨论】:
在 .net 中,您可以从 System.Diagnostics 中 Process.Start 启动应用程序,甚至可以传递参数,并且可以使用 P/Invoke 模拟鼠标事件,SO here 上已经有答案
【讨论】:
如果可以的话,作为充满鳗鱼的气垫船 - 使用 autoit - 会容易得多。如果 AutoIt 不是一个选项,那么您将需要使用 winAPI 函数来执行此操作。
例如在坐标处调用 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
}
但请注意,要在窗口内进行点击,它需要在您面前 - 例如,您不能点击最小化的应用程序。
如果你想尝试 - 你可以在PInvoke找到所有需要的功能(如何运行程序,通过hwnd获取需要的窗口等等)
【讨论】:
【讨论】:
java.awt.Robot 可以点击屏幕上的按钮,但不会与操作系统进行低级别交互。
这是我的工作测试应用程序,可以在 Windows 中单击。 我们只是启动一些应用程序并希望在正确的位置单击它) 最好有一些解决方案来以这种方式捕获窗口 =)
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;
}
}
【讨论】: