Hello. I have encountered the following issue. In Visual Studio 2022 (1-st and 2-nd images), I was able to parse window elements using the AutomationElements library in C#. However, in Visual Studio 2015 (3-rd image), this does not work because the tree-grid element structure is broken.
Code:
AutomationElement mainWindow = AutomationElement.FromHandle((IntPtr)customToolWindow.HWnd);
PrintAutomationElement(mainWindow, ref variables);
private void GetElementsFromTreeGreed(AutomationElement element, ref ObservableCollection<Variable> variables)
{
string name = element.Current.Name;
if (element.Current.ClassName == "TreeGridItem")
{
var expressionForTypeAndName = m_DTE_Dte.Debugger.GetExpression(name, true, 1);
var expressionForAddress = m_DTE_Dte.Debugger.GetExpression("&(" + name + ")", true, 1);
if (expressionForTypeAndName.IsValidValue && expressionForAddress.IsValidValue)
{
Variable variable = new Variable()
{
m_B_IsAdded = false,
m_B_IsSelected = false,
m_S_Name = expressionForTypeAndName.Name,
m_S_Source = "WatchWindow",
m_S_Type = expressionForTypeAndName.Type.Replace(" ", ""),
m_S_Addres = expressionForAddress.Value.Split(' ')[0],
m_C_Color = new Utils.Color(0, 255, 0)
};
if (!isContainVariable(variable, variables))
variables.Add(variable);
else
System.Windows.MessageBox.Show("ERROR: A variable with this address: " + variable.m_S_Addres + " is already in the table.\nIt will not be added to it.");
}
}
else
return;
}
private void PrintAutomationElement(AutomationElement element, ref ObservableCollection<Variable> variables)
{
if (element.Current.ClassName == "TreeGrid")
{
var children = element.FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);
foreach (AutomationElement child in children)
if (child.Current.ClassName == "TreeGridItem")
GetElementsFromTreeGreed(child, ref variables);
}
else
{
var children = element.FindAll(TreeScope.Children, System.Windows.Automation.Condition.TrueCondition);
foreach (AutomationElement child in children)
PrintAutomationElement(child, ref variables);
}
}