请耐心等待,我是一名尝试WinForms应用程序的Web开发人员。
在回复时,请不要评论我的申请的目的是否是最佳实践"是不是等等......这是我需要写的应用程序,无论好坏,我都不能浪费时间来解释"为什么"。我请你回复一下建议:(1)我试图做的事情(2)什么是最好的"如何实现我想要做的事情。
所以,这就是我要做的事情: 我需要编写一个WinForms应用程序,它将从供应商应用程序中读取数据,向最终用户显示某些记录,并允许他们选择" transfer"另一个供应商应用程序的记录。挂断...目标系统没有暴露apis,没有sprocs,没有提供"好"插入数据的方法,而不是在GUI中输入数据。 (相信我,一个由3人组成的团队调查了自动化的可能性)
因此,此WinForms应用程序将使用Microsoft.VisualBasic.Interaction.AppActivate(字符串)并将数据直接插入目标系统的输入窗口中的字段。目标系统在某些字段上有F1帮助,因此如果您将焦点放在该字段上并按" F1",或者如果您在其中一个字段中输入无效数据,则会将一个名为&#34的子窗口;帮助"弹出提供指导。
所以,我的应用程序正在插入,使用SendKeys.SendWait(字符串)将数据写入此应用程序的字段,然后遇到一些无效数据并弹出帮助窗口,我的应用程序继续写出其余的数据,但现在它全部被写入帮助窗口中的1字段,因为该窗口现在具有焦点。
百万美元的问题......
有没有办法从我的WinForms应用程序中检测到目标系统中的“帮助”窗口已被激活,以便我可以解决它?
更新
首次尝试此应用程序我使用了UIAutomationClient库。我无法成功激活目标窗口并写入第一个字段。经过一天的战斗,我不得不开始寻找其他选择。
答案 0 :(得分:2)
我有一些工作代码的开头。我想我已经足够了,这可能对其他人有用。
我找到了一种使用UIAutomation库而不是Microsoft.VisualBasic库的方法。有很多试验和错误,但这比我使用VisualBasic库的前几次尝试要“更加光滑”。
我使用StructureChanged事件来检测在此概念证明应用程序的上一次迭代中弹出的“帮助”窗口。我能够成功检测到弹出的“帮助”窗口,但是我遇到了试图处理“帮助”窗口外观的障碍。我的应用程序中仍然有事件处理程序,但由于某种原因,现在不会弹出“帮助”窗口,我尝试将无效数据强制插入其中一个字段以测试“帮助”窗口的处理,并且未显示“帮助”窗口。这对我来说不是问题,因为帮助窗口给我带来了麻烦,但我觉得需要提及它,因为这篇文章询问我如何能够检测到帮助窗口的外观。
我使用的一些资源:
http://msdn.microsoft.com/en-us/library/ms747327%28v=vs.110%29.aspx
https://www.universalthread.com/ViewPageArticle.aspx?ID=199
http://msdn.microsoft.com/en-us/library/ms750582%28v=vs.110%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd318521%28v=vs.85%29.aspx
private void IncidentGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex < 0) || (e.ColumnIndex < 0))
return;
// Grab the data object that populated the row
Incident incident = (Incident)IncidentGridView.Rows[e.RowIndex].DataBoundItem;
// Create a property condition with the element's type
PropertyCondition typeCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
// Create a property condition with the element's name
PropertyCondition nameCondition = new PropertyCondition(AutomationElement.NameProperty, "Incident");
// Create the conjunction condition
AndCondition andCondition = new AndCondition(typeCondition, nameCondition);
// Ask the Desktop to find the element within its children with the given condition
_mainWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, andCondition);
#region Register Automation Events
//AutomationEventHandler handler = new AutomationEventHandler(OnWindowOpened);
//Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, _mainWindow, TreeScope.Element, handler);
Automation.AddStructureChangedEventHandler(_mainWindow, TreeScope.Children, new StructureChangedEventHandler(OnStructureChanged));
#endregion
// Wait for the application
Thread.Sleep(2000);
// Write the incident to the Incident window
PropertyCondition idCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "3279");
AutomationElement reportedDate = _mainWindow.FindFirst(TreeScope.Descendants, idCondition);
InsertTextUsingUIAutomation(reportedDate, incident.ReportedDate);
PropertyCondition idCondition2 = new PropertyCondition(AutomationElement.AutomationIdProperty, "4256");
AutomationElement reportedTime = _mainWindow.FindFirst(TreeScope.Descendants, idCondition2);
InsertTextUsingUIAutomation(reportedTime, incident.ReportedTime);
PropertyCondition idCondition4 = new PropertyCondition(AutomationElement.AutomationIdProperty, "7256");
AutomationElement status = _mainWindow.FindFirst(TreeScope.Descendants, idCondition4);
InsertTextUsingUIAutomation(status, "WAR");
PropertyCondition idCondition5 = new PropertyCondition(AutomationElement.AutomationIdProperty, "5404");
AutomationElement natureOfCall = _mainWindow.FindFirst(TreeScope.Descendants, idCondition5);
InsertTextUsingUIAutomation(natureOfCall, "TRAF WARN");
PropertyCondition idCondition11 = new PropertyCondition(AutomationElement.AutomationIdProperty, "4935");
AutomationElement location = _mainWindow.FindFirst(TreeScope.Descendants, idCondition11);
InsertTextUsingUIAutomation(location, incident.Location);
PropertyCondition idCondition12 = new PropertyCondition(AutomationElement.AutomationIdProperty, "2876");
AutomationElement city = _mainWindow.FindFirst(TreeScope.Descendants, idCondition12);
InsertTextUsingUIAutomation(city, incident.City);
PropertyCondition idCondition15 = new PropertyCondition(AutomationElement.AutomationIdProperty, "5693");
AutomationElement officer = _mainWindow.FindFirst(TreeScope.Descendants, idCondition15);
InsertTextUsingUIAutomation(officer, incident.Officer);
PropertyCondition idCondition19 = new PropertyCondition(AutomationElement.AutomationIdProperty, "4023");
AutomationElement fromDate = _mainWindow.FindFirst(TreeScope.Descendants, idCondition19);
InsertTextUsingUIAutomation(fromDate, incident.FromDate);
PropertyCondition idCondition20 = new PropertyCondition(AutomationElement.AutomationIdProperty, "4042");
AutomationElement fromTime = _mainWindow.FindFirst(TreeScope.Descendants, idCondition20);
InsertTextUsingUIAutomation(fromTime, incident.FromTime);
PropertyCondition idCondition21 = new PropertyCondition(AutomationElement.AutomationIdProperty, "7556");
AutomationElement toDate = _mainWindow.FindFirst(TreeScope.Descendants, idCondition21);
InsertTextUsingUIAutomation(toDate, incident.ToDate);
PropertyCondition idCondition22 = new PropertyCondition(AutomationElement.AutomationIdProperty, "7576");
AutomationElement toTime = _mainWindow.FindFirst(TreeScope.Descendants, idCondition22);
InsertTextUsingUIAutomation(toTime, incident.ToTime);
PropertyCondition idCondition30 = new PropertyCondition(AutomationElement.AutomationIdProperty, "2001");
AutomationElement remarks = _mainWindow.FindFirst(TreeScope.Descendants, idCondition30);
InsertTextUsingUIAutomation(remarks, incident.Remarks);
MessageBox.Show("Incident was transferred.");
}
这是StructureChangedEvent的事件处理程序:
private void OnStructureChanged(object sender, StructureChangedEventArgs e)
{
AutomationElement element = sender as AutomationElement;
if (e.StructureChangeType == StructureChangeType.ChildAdded)
{
Object windowPattern;
if (false == element.TryGetCurrentPattern(WindowPattern.Pattern, out windowPattern))
return;
if (element.Current.Name == "Help")
{
// How do we want to handle this???
MessageBox.Show("Waiting 30 seconds to allow user to resolve data issue.");
Thread.Sleep(30000);
}
}
}