简体   繁体   English

如何以编程方式(C#或Java)在Windows中启动应用并在其窗口中调用click?

[英]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#中...

  1. Use the Process class to start the process (there are plenty of resources on the web on how to do this. 使用Process类来启动该过程(网络上有很多有关如何执行此操作的资源。
  2. Wait until the process has started (either just wait for a fixed amount of time which is probably going to be long enough, or you could try and do something fancy like IPC or monitoring for a window being created) 等待该过程开始(要么等待一段固定的时间,这可能会足够长,或者您可以尝试执行一些类似于IPC的操作或监视正在创建的窗口)
  3. To simulate the click take a look at How to simulate Mouse Click in C#? 要模拟点击,请看一下如何在C#中模拟鼠标点击? which uses a P/Invoke call to the mouse_event function. 它使用对mouse_event函数的P / Invoke调用。

However note that there are several things that can go wrong with this 但是请注意,这可能会导致一些错误

  • Someone might move the window, or place another window on top of that window in the time it takes to launch the application 有人可能会移动窗口,或者在启动应用程序时将另一个窗口放在该窗口的顶部
  • On a slower PC it may take longer to load the application (this risk can be mitigated by doing things like monitoring open windows and waiting for the expected application window to appear) 在速度较慢的PC上,加载应用程序可能需要更长的时间(可以通过监视打开的窗口并等待预期的应用程序窗口出现来减轻这种风险)

在.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.

相关问题 如何在C#中以编程方式启动chkdsk GUI窗口? - How can I programmatically launch chkdsk GUI window in C#? 如何通过C#表单应用程序启动Windows8 Metro App? - How to launch a Windows8 Metro App by C# form application? 如何使用 C# 从 Windows 窗体启动 UWP 应用程序 - How to launch UWP app from windows forms using C# 如何在 C# 窗口应用程序中以编程方式创建按钮? - How can I create a button programmatically in C# window app? 如何在 C# 中以编程方式启动 atmx 文件? - How to launch the atmx file programmatically in C#? Windows 8 Button在C#和Xaml中以编程方式单击 - Windows 8 Button click programmatically in C# and Xaml 如何使用C#代码以编程方式单击运行应用程序中的按钮 - how to programmatically click on a button in running app using C# code 如何通过 C# 中的按钮单击事件以编程方式启动 dsa.msc? - How can you launch dsa.msc programmatically through a button click event in C#? 如何在通用Windows应用程序C#中以编程方式使用setter - How to use setter programmatically in universal windows app c# 如何以编程方式从我的 Windows 10 应用程序启动相机应用程序? - How to launch camera app from my windows 10 app programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM