如果使用inspect软件也看不到你所谓edit控件对应的窗口句柄,那就别指望代码能获取了。
没准人家用的是自绘,根本没有对应的窗口,何来窗口句柄一说?
你既然提到inspect软件可以,那你有没有注意inspect软件采用的是UI Automation技术?
百度搜“UI Automation”
仅供参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Automation;
namespace CStest
{
class Program
{
static void Main(string[] args)
{
//System.Diagnostics.Process.Start("calc");
//Thread.Sleep(3000); //Sleep 3 sconds to wait calculator launched
try
{
//Get the destkop element
AutomationElement elemDesktop = AutomationElement.RootElement;
//Search the Application main window by title from all children
PropertyCondition pCondition = new PropertyCondition(AutomationElement.NameProperty, "计算器");
AutomationElement elemApplicationWindow = elemDesktop.FindFirst(TreeScope.Children, pCondition);
//Search the 1 button
AutomationElement btnOne = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "一"));
InvokePattern invokePattern1 = btnOne.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern1.Invoke();
AutomationElement btnPlus = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "加"));
InvokePattern invokePatternP = btnPlus.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePatternP.Invoke();
AutomationElement btnTwo = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "二"));
InvokePattern invokePattern2 = btnTwo.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern2.Invoke();
AutomationElement btnE = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "等于"));
InvokePattern invokePatternE = btnE.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePatternE.Invoke();
//Verify the result by get the Name property
AutomationElement labelResult = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "显示为 3"));
if (labelResult.Current.Name == "显示为 3")
{
Console.WriteLine("Test case pass!");
}
else
{
Console.WriteLine("Test case fail!");
}
}
catch (Exception)
{
Console.WriteLine("Test case error!");
}
}
}
}