简体   繁体   English

等待应用程序窗口打开然后执行一些操作

[英]Wait for application window to open then do something

I am very new to C# but I am fast learning. 我是C#的新手,但是我正在快速学习。 I have seen various answers to this but I'm not sure they suite, or maybe I'm just not getting them. 我已经看到了各种答案,但是我不确定它们是否适用,或者我只是没有得到它们。

I have a business requirement to write ac# app that sites in the system tray and runs when a certain action has been performed in Internet Explorer. 我有一个业务需求,要编写一个位于系统托盘中并在Internet Explorer中执行某些操作后才能运行的ac#应用程序。 This action (for now) is the opening of the about internet explorer window. 此操作(目前)是About Internet Explorer窗口的打开。 I need the app to constantly monitor to see if this window is open and then run some code, an example would be close the about window and display a message box. 我需要应用程序不断监视以查看此窗口是否打开,然后运行一些代码,例如关闭“关于”窗口并显示消息框。

I have managed to get the system tray part working, how would i go about monitoring that the window exists? 我设法使系统托盘部分正常工作,如何监视窗口是否存在?

EDIT To answer some of the questions in the comments: At work we use a booking system that only runs in IE, we are looking for a way to automate a section of our manual process, to do this we are thinking that we can have the following process: go to booking, click help > about, system tray app closes the window and runs some code to update the database. 编辑要回答评论中的一些问题:在工作中,我们使用仅在IE中运行的预订系统,我们正在寻找一种使手动过程的一部分自动化的方法,为此,我们认为我们可以步骤如下:去预订,单击“帮助”>“关于”,系统任务栏应用程序关闭窗口并运行一些代码以更新数据库。 that's pretty much it. 就是这样。

I have had a look at some of the following: How to check if window is opned and close it c#: How do check if another application window is open on my machine (ie, iterate through all open windows)? 我看过以下一些内容: 如何检查是否选择了窗口并关闭它 c#:如何检查我的计算机上是否打开了另一个应用程序窗口(即,对所有打开的窗口进行迭代)?

I think i understand that I have to look through the running processes and check there relevant programs properties to see if it has the correct window name? 我想我知道我必须仔细检查正在运行的进程,并检查那里的相关程序属性,以查看其是否具有正确的窗口名称? Im sure i can get that to work (eventually) however it was the waiting for that window to open part that i am stuck with, does C# have a waitfor or do i have to write a function to constantly check that window is open? 我确定我可以使它(最终)正常工作,但是那是等待该窗口打开的卡住的部分,C#是否有waitfor,还是我必须编写一个函数来不断检查该窗口是否打开?

EDIT 编辑

I have accepted @Adrian's answer as it looks like it will do want i wanted; 我已经接受@Adrian的回答,因为它看起来确实想要我想要的; however after reading all the comments I went to my manager and we discussed this. 但是,在阅读完所有评论之后,我去找了我的经理,我们进行了讨论。 We will now go down the route of having the user enter the booking number into the C# App its self via a text box in the right click menu. 现在,我们将沿着以下路线进行操作:让用户通过右键单击菜单中的文本框在C#App中自行输入预订号。 Thanks all. 谢谢大家

You probably want to use the FindWindowEx API call 您可能要使用FindWindowEx API调用

See here for example code: 请参阅此处的示例代码:

http://www.pinvoke.net/default.aspx/user32/findwindowex.html http://www.pinvoke.net/default.aspx/user32/findwindowex.html

Then SendMessage to close the window. 然后SendMessage关闭窗口。

http://www.pinvoke.net/default.aspx/user32/SendMessage.html http://www.pinvoke.net/default.aspx/user32/SendMessage.html

But you will be limited to what events you can capture/use from other applications...for security reasons. 但是出于安全原因,您将受限于可以从其他应用程序捕获/使用的事件...。 Unless the app specifically allows such interactions. 除非该应用明确允许此类互动。

What you can do is to use AutoIt. 您可以做的就是使用AutoIt。 It is a freeware desktop automation tool that also offers a .NET class library for integration. 它是一个免费的桌面自动化工具,还提供一个用于集成的.NET类库。 With this, you can check and wait until a certain window is opened fairly easy. 这样,您可以检查并等待,直到相当容易地打开某个窗口。

You can download this tool from here: https://www.autoitscript.com 您可以从此处下载此工具: https : //www.autoitscript.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM