0

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);
}
  • It would help to know where you got `ForegroundHandle` from and whether you've verified that it is indeed a handle to the window you think it is. – Damien_The_Unbeliever Jul 07 '23 at 05:59
  • From https://devblogs.microsoft.com/math-in-office/windows-11-notepad/ "The new Windows 11 Notepad uses RichEdit" which probably means you're looking for the wrong thing. – Retired Ninja Jul 07 '23 at 06:00
  • @RetiredNinja I tried your suggestion a moment ago, it did not work – oyashchyshyn Jul 07 '23 at 06:03
  • [Spy++](https://learn.microsoft.com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2022) us your friend. If you read the first article I linked they are not using a standard RichEdit control. – Retired Ninja Jul 07 '23 at 06:04
  • @RetiredNinja When I am using Spy++ tool, it does not show any data when navigating on notepad edit area – oyashchyshyn Jul 07 '23 at 06:11
  • @Damien_The_Unbeliever I am getting it from GetForegroundWindow https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow – oyashchyshyn Jul 07 '23 at 14:10

0 Answers0