English 中文(简体)
使用 C# WinForms 应用程序访问其它应用程序元素
原标题:accessing another app s elements using C# WinForms app

我希望能够在另一项申请中采取以下行动:

  • to change Tabs
  • to Copy text from within a TextBox
  • to click on a Button
  • to enter text into a TextBox
  • to select DropDownList element

我现在使用不同的方法,例如:

  • mouse_event() to change mouse coordinates and click on a button
  • another mouse_event(LeftMouseClick followed by the RightMouseClick) to copy a text within a TextBox
  • Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text) to Copy what s inside the clipboard
  • SendInput (for each key) - enter the text into a TextBox

这种办法的缺点是:

  • (not crucial) PC becomes unusable (you can t work while script is running)
  • I have to know exact pixels (read - position/coordinates) of EVERY element within an app
  • slow execution time (each key has to be typed separately)

我期待创建一个应用程序,可以点击文本框/按钮/列表 ,而不需要这些元素的精确坐标

Is such task possible with C# WinForms? My current approach works but it has it s flaws. Any advice?

问题回答

有关Mutex 。如果你计划规模化,阅读网络通信(例如,TCP协议)。TcClient )。

这听起来像是"的工作 http://msdn.microsoft.com/ en- us/library/ms747327.aspx" rel = “nofollow” >UI Automation 。 我仅用它从另一个应用程序获取文本, 但它有功能按名称激活控制或在没有名称的情况下浏览控制树 。

您可以使用 < a href=> http://msdn.microsoft.com/ en- us/library/ system.windows.automation.automation.automationement.aspx" rel = "nofollow" > AutomationElement 获得文本并与控件进行互动。

有一个完整的框架来完成这类事情, 它叫做 < a href=" http://msdn.microsoft.com/ en- us/library/aa348551" rel=“ nofollow” >UI Automation 框架

Here s some examples on how to use it.
And you can also apply this technique to generic windows s using the UI Spy to determine the automation elements.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签