@@ -867,10 +867,23 @@ public bool PreFilterMessage(ref Message m)
867867 User32 . SetCursor ( cursorHandle ) ;
868868 var cursorPosition = Cursor . Position ;
869869 var element = AutomationElement . FromPoint ( new System . Windows . Point ( cursorPosition . X , cursorPosition . Y ) ) ;
870- if ( element != null && element . Current . ProcessId != _processId )
870+ var windowProcessId = element . Current . ProcessId ;
871+ if ( element != null && windowProcessId != _processId )
871872 {
872873 var windowHandle = new IntPtr ( element . Current . NativeWindowHandle ) ;
873- windowHandle = windowHandle == IntPtr . Zero ? User32 . WindowFromPoint ( new Point ( cursorPosition . X , cursorPosition . Y ) ) : windowHandle ;
874+ if ( windowHandle == IntPtr . Zero )
875+ {
876+ windowHandle = User32 . WindowFromPoint ( new Point ( cursorPosition . X , cursorPosition . Y ) ) ;
877+ if ( windowHandle != IntPtr . Zero )
878+ {
879+ User32 . GetWindowThreadProcessId ( windowHandle , out windowProcessId ) ;
880+ var windowStyle = User32 . GetWindowLong ( windowHandle , Constants . GWL_STYLE ) ;
881+ if ( ( ( windowStyle & Constants . WS_CAPTION ) != 0 || ( windowStyle & Constants . WS_SYSMENU ) != 0 || ( windowStyle & Constants . WS_POPUP ) != 0 ) )
882+ {
883+ element = AutomationElement . FromHandle ( windowHandle ) ;
884+ }
885+ }
886+ }
874887
875888 var previousHandle = IntPtr . Zero ;
876889 var previousProcessId = 0 ;
@@ -898,7 +911,7 @@ public bool PreFilterMessage(ref Message m)
898911 return false ;
899912 }
900913
901- using var process = Process . GetProcessById ( element . Current . ProcessId ) ;
914+ using var process = Process . GetProcessById ( windowProcessId ) ;
902915 if ( element . Current . IsPassword )
903916 {
904917 if ( process . ProcessName . ToLower ( ) == "iexplore" )
@@ -933,7 +946,7 @@ public bool PreFilterMessage(ref Message m)
933946 }
934947 else
935948 {
936- var text = element . GetTextFromConsole ( ) ?? element . GetTextFromWindow ( ) ;
949+ var text = WindowUtils . ExtractTextFromConsoleWindow ( windowProcessId ) ?? element . GetTextFromWindow ( ) ;
937950 text = text == null ? "" : text . TrimEnd ( ) . TrimEnd ( Environment . NewLine ) ;
938951 if ( _settings . ShowEmptyItems || ( ! _settings . ShowEmptyItems && ! string . IsNullOrEmpty ( text ) ) )
939952 {
@@ -947,7 +960,7 @@ public bool PreFilterMessage(ref Message m)
947960 lock ( _lockObject )
948961 {
949962 _windowHandle = windowHandle ;
950- _windowProcessId = element . Current . ProcessId ;
963+ _windowProcessId = windowProcessId ;
951964 scale = _settings . Scale ;
952965 captureCursor = _settings . CaptureCursor ;
953966 }
0 commit comments