I tried
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
public static AutomationElement GetEdgeCommandsWindow(AutomationElement edgeWindow)
{
return edgeWindow.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
}
public static string GetEdgeUrl(AutomationElement edgeCommandsWindow)
{
var adressEditBox = edgeCommandsWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
}
public static string GetEdgeTitle(AutomationElement edgeWindow)
{
var adressEditBox = edgeWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"));
return adressEditBox.Current.Name;
}
[DllImport("user32")]
public static extern IntPtr GetDesktopWindow();
public enum TreeScope { }
private void timer1_Tick(object sender, EventArgs e)
{
AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
foreach (AutomationElement child in main.FindAll(TreeScope.Children, Condition.TrueCondition))
{
AutomationElement window = GetEdgeCommandsWindow(child);
if (window == null) // not edge
continue;
}
}
}
}
But AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
foreach (AutomationElement child in main.FindAll(TreeScope.Children, Condition.TrueCondition))
shows error and message is THE TYPE 'TREESCOPE' IS DEFINED IN AN ASSEMBLY THAT IS NOT REFERED. YOU MUST ADD A REFERENCE TO ASSEMBLY 'UIAUTOMATIONTYPES VERSION:4.0.0.0 CULTURE = NEUTRAL'
If anyone knows how to solve this issue please do comment