Maybe you can test with UIAutomationCore and IUIAutomation :
(I don't have Windows 11, so only tested on Windows 10)
// Add reference to : "C:\Windows\system32\UIAutomationCore.dll" (UIAutomationClient : Embed Interop Types = False)
// Add : using UIAutomationClient;
// Add : using System.Runtime.InteropServices;
var p = System.Diagnostics.Process.GetProcessesByName("notepad").FirstOrDefault();
IntPtr hWndNotepad = p.MainWindowHandle;
if (hWndNotepad != IntPtr.Zero)
{
IUIAutomation pUIAutomation = new CUIAutomation8();
if (pUIAutomation != null)
{
IUIAutomationTextPattern pTextPattern = null;
IUIAutomationElement pWindowElement = pUIAutomation.ElementFromHandle(hWndNotepad);
IUIAutomationCacheRequest pCacheRequest = pUIAutomation.CreateCacheRequest();
if (pCacheRequest != null)
{
pCacheRequest.AddProperty(UIA_PropertyIds.UIA_IsTextPatternAvailablePropertyId);
pCacheRequest.AddPattern(UIA_PatternIds.UIA_TextPatternId);
IUIAutomationCondition pCondition = pUIAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_DocumentControlTypeId);
if (pCondition != null)
{
IUIAutomationElement pElement = pWindowElement.FindFirstBuildCache(TreeScope.TreeScope_Children, pCondition, pCacheRequest);
if (pElement != null)
{
bool bRet = (bool)pElement.GetCachedPropertyValue(UIA_PropertyIds.UIA_IsTextPatternAvailablePropertyId);
if (bRet)
{
pTextPattern = (IUIAutomationTextPattern)pElement.GetCachedPattern(UIA_PatternIds.UIA_TextPatternId);
if (pTextPattern != null)
{
IUIAutomationTextRangeArray pTextRangeArray = pTextPattern.GetSelection();
if (pTextRangeArray != null)
{
string sText = "";
int nNbItems = pTextRangeArray.Length;
for (int nItem = 0; nItem <= nNbItems - 1; nItem++)
{
IUIAutomationTextRange element = pTextRangeArray.GetElement(nItem);
sText += element.GetText(-1);
}
Console.WriteLine("Selected text = " + sText);
Marshal.ReleaseComObject(pTextRangeArray);
}
Marshal.ReleaseComObject(pTextPattern);
}
}
Marshal.ReleaseComObject(pElement);
}
Marshal.ReleaseComObject(pCondition);
}
Marshal.ReleaseComObject(pCacheRequest);
}
Marshal.ReleaseComObject(pWindowElement);
Marshal.ReleaseComObject(pUIAutomation);
}
}