I made a minimal sample with ElementFromPoint to get IUIAutomationElement under the mouse,
you can check if it crashes too (cannot test on my OS (Windows 10 1909)=>
Reference + using :
// Add reference to : C:\Windows\System32\UIAutomationCore.dll (UIAutomationClient : Embed Interop Types = False)
// Add : using UIAutomationClient;
Test :
public partial class Form1 : Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Threading.Thread workerThread = null;
private readonly System.Threading.AutoResetEvent isCanceled = new System.Threading.AutoResetEvent(false);
IUIAutomation uiA = null;
public Form1()
{
//InitializeComponent();
this.Text = "Form1";
this.ClientSize = new System.Drawing.Size(540, 140);
this.Load += new System.EventHandler(this.Form1_Load);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
}
private void Form1_Load(object sender, EventArgs e)
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox1.Location = new System.Drawing.Point(20, 20);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(500, 100);
this.richTextBox1.ReadOnly = true;
this.richTextBox1.Text = "";
Controls.Add(this.richTextBox1);
uiA = new CUIAutomation();
workerThread = new System.Threading.Thread(BackgroundTask);
workerThread.IsBackground = true;
workerThread.Name = "Background Task";
workerThread.Start();
CenterToScreen();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
workerThread.Abort();
}
private void BackgroundTask()
{
for (; !isCanceled.WaitOne(0);)
{
System.Drawing.Point point = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y);
tagPOINT tp = new tagPOINT();
tp.x = (int)point.X;
tp.y = (int)point.Y;
string sCurrentName = null;
string sCurrentType = null;
string sCurrentValue = null;
string sCurrentText= null;
// UIA_E_ELEMENTNOTAVAILABLE {"Un événement n’a pu invoquer aucun des abonnés. (Exception de HRESULT : 0x80040201)"} System.Runtime.InteropServices.COMException
try
{
IUIAutomationElement element = uiA.ElementFromPoint(tp);
if (element != null)
{
sCurrentName = element.CurrentName;
sCurrentType = element.CurrentLocalizedControlType;
Object pPatternValue = element.GetCurrentPattern(UIA_PatternIds.UIA_ValuePatternId);
if (pPatternValue != null)
{
IUIAutomationValuePattern pValuePattern = (IUIAutomationValuePattern)pPatternValue;
sCurrentValue = pValuePattern.CurrentValue;
}
}
sCurrentText = (sCurrentName != null ? sCurrentName : "");
sCurrentText += (sCurrentValue != null ? Environment.NewLine + sCurrentValue : "");
sCurrentText += (sCurrentType != null ? Environment.NewLine + sCurrentType : "");
if (element.CurrentAutomationId != "richTextBox1")
SetText(richTextBox1, sCurrentText);
else
SetText(richTextBox1, "RichEdit Control" + Environment.NewLine + "richTextBox1");
}
catch (System.Exception ex)
{
string sErrorMsg = ex.ToString();
}
}
}
delegate void SetTextCallback(Control ctrl, string text);
private void SetText(Control ctrl, string sText)
{
if (ctrl.InvokeRequired)
{
SetTextCallback c = new SetTextCallback(SetText);
this.Invoke(c, new object[] { ctrl, sText });
}
else
{
ctrl.Text = sText;
}
}
}