Cannot get handle of Edit control from notepad using Windows 11

273 views Asked by At

I have several ways to get notepad's edit control handle:

  1. Using user32
FindWindowEx(ForegroundHandle, IntPtr.Zero, "Edit", null);
  1. Using Automation
var notepadWindow = AutomationElement.FromHandle(ForegroundHandle);

var editControl = notepadWindow.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"));

This 2 ways perfectly work using Windows 10 but when it comes to Windows 11 it's not working, none of them.

I am expecting this should work with Windows 11 in the same ways as it is with Windows 10. During invetigation I have found article about preview build: https://blogs.windows.com/windows-insider/2022/10/27/announcing-windows-11-insider-preview-build-25231/ where it states:

Fixed a rare issue where FindWindow and FindWindowEx might return an unexpected window.

But I am not sure that this is relates to my problem, because I am returning IntPtr.Zero or null all the time using approaches above.

UPDATE

I found what is the problem, instead of using "Edit" in FindWindowEx, you need to use "RichEditD2DPT" for Notepad in Windows 11

var notepadTextBoxHandle= _winApiService.FindWindowEx(NotepadForegroundHandle, IntPtr.Zero, "NotepadTextBox", null);

if (notepadTextBoxHandle != IntPtr.Zero)
{
    _editControl = _winApiService.FindWindowEx(NotepadForegroundHandle, IntPtr.Zero, "RichEditD2DPT", null);
}
0

There are 0 answers