使用DLL名称获取表单数据

我正在创build一个应用程序的工具,打开一些窗体来获取用户的信息,我的工具应该自己处理这些Windows窗体,而无需用户的交互。 我已经启动了一个事件来获取打开的窗体的过程,当它打开时由以下代码:

mgmtWtch = new ManagementEventWatcher("Select * From Win32_ProcessStartTrace"); mgmtWtch.EventArrived += WatchManagementEvent; mgmtWtch.Start(); 

显示的窗口有我想单击的确定button,我不知道如何做这个动作。 而我可以从这个事件得到的参数是

EventArrivedEventArgs e

我的问题是我怎样才能通过这个事件处理程序单击确定button?

提前致谢。

你看过.Net的GUI自动化API吗?

您将需要UIAutomationClientUIAutomationTypes程序集。

我已经使用这个API来驱动安装程序,测试期间的用户界面等。

我发现这个链接最初有用。

http://blogs.msdn.com/b/oldnewthing/archive/2013/04/08/10409196.aspx

例如,假设您有按钮的父窗口(即窗体),并且您知道该按钮的ID:

 using System.Windows.Automation; .... static AutomationElement FindById(AutomationElement root, string id, bool directChild) { Assert(root != null, "Invalid input: ParentWindow element 'root' is null."); Condition conditions = new PropertyCondition(AutomationElement.AutomationIdProperty, id); return root.FindFirst(directChild ? TreeScope.Children : TreeScope.Descendants, conditions); } .... AutomationElement button = FindById(containerWindow, id.ToString(), true); InvokePattern invokePattern = null; try { invokePattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; } catch (InvalidOperationException) { MessageBox.Show("The UI element named " + button.GetCurrentPropertyValue(AutomationElement.NameProperty) + " is not a button"); return false; } invokePattern.Invoke(); 

如果您不知道该按钮的ID,但知道它的名称,即按钮上的文本,然后将AutomationElement.AutomationIdProperty替换为FindById中的FindById (并适当地重命名该方法)

假设该按钮位于顶层窗体窗口中,并且您知道在此窗体窗口中显示的标题,则以下代码将获得该按钮的父窗口:

 bool ignoreCase = true; // or false if preferred Condition conditions = new PropertyCondition( AutomationElement.NameProperty, windowTitle, ignoreCase ? PropertyConditionFlags.IgnoreCase : PropertyConditionFlags.None ); AutomationElement myForm = AutomationElement.RootElement.FindFirst( TreeScope.Children, conditions ); 

窗口标题可以通过进程的MainWindowTitle属性从已有的进程中获取。