Skip to content

Commit c54fc47

Browse files
Updated code for getting the active window from outside the library.
Made the 'GetAbsoluteActiveWindow()' method public and added the 'GetActiveWindow()' method.
1 parent c160fbd commit c54fc47

1 file changed

Lines changed: 39 additions & 29 deletions

File tree

InputHelper Library/InputHelper Library/InputHelper.vb

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,45 @@ Public NotInheritable Class InputHelper
616616
End Sub
617617
#End Region
618618

619+
#Region "GetActiveWindow()"
620+
''' <summary>
621+
''' Gets the active top-level window.
622+
''' </summary>
623+
''' <remarks></remarks>
624+
Public Shared Function GetActiveWindow() As IntPtr
625+
Return NativeMethods.GetForegroundWindow()
626+
End Function
627+
#End Region
628+
629+
#Region "GetAbsoluteActiveWindow()"
630+
''' <summary>
631+
''' Gets the (absolute) active top-level window or child window (apart from GetActiveWindow() which will only get the active top-level window).
632+
''' </summary>
633+
''' <remarks></remarks>
634+
Public Shared Function GetAbsoluteActiveWindow() As IntPtr
635+
Dim CurrentThreadID As UInteger = NativeMethods.GetCurrentThreadId()
636+
Dim ActiveWindow As IntPtr = NativeMethods.GetForegroundWindow()
637+
Dim ActiveThread As UInteger = NativeMethods.GetWindowThreadProcessId(ActiveWindow, Nothing)
638+
639+
If ActiveThread = 0 Then _
640+
Return ActiveWindow
641+
642+
If NativeMethods.AttachThreadInput(CurrentThreadID, ActiveThread, True) = False Then _
643+
Return ActiveWindow
644+
645+
Dim AbsoluteActiveWindow As IntPtr = NativeMethods.GetFocus()
646+
Dim DetachAttempts As Integer = 0
647+
648+
While NativeMethods.AttachThreadInput(CurrentThreadID, ActiveThread, False) = False
649+
DetachAttempts += 1
650+
If DetachAttempts >= 10 Then Exit While
651+
Threading.Thread.Sleep(1)
652+
End While
653+
654+
Return AbsoluteActiveWindow
655+
End Function
656+
#End Region
657+
619658
#End Region
620659

621660
#Region "Internal methods"
@@ -855,35 +894,6 @@ Public NotInheritable Class InputHelper
855894
End Function
856895
#End Region
857896

858-
#Region "GetAbsoluteActiveWindow()"
859-
''' <summary>
860-
''' Gets the absolute active window or child window.
861-
''' </summary>
862-
''' <remarks></remarks>
863-
Private Shared Function GetAbsoluteActiveWindow() As IntPtr
864-
Dim CurrentThreadID As UInteger = NativeMethods.GetCurrentThreadId()
865-
Dim ActiveWindow As IntPtr = NativeMethods.GetForegroundWindow()
866-
Dim ActiveThread As UInteger = NativeMethods.GetWindowThreadProcessId(ActiveWindow, Nothing)
867-
868-
If ActiveThread = 0 Then _
869-
Return ActiveWindow
870-
871-
If NativeMethods.AttachThreadInput(CurrentThreadID, ActiveThread, True) = False Then _
872-
Return ActiveWindow
873-
874-
Dim AbsoluteActiveWindow As IntPtr = NativeMethods.GetFocus()
875-
Dim DetachAttempts As Integer = 0
876-
877-
While NativeMethods.AttachThreadInput(CurrentThreadID, ActiveThread, False) = False
878-
DetachAttempts += 1
879-
If DetachAttempts >= 10 Then Exit While
880-
Threading.Thread.Sleep(1)
881-
End While
882-
883-
Return AbsoluteActiveWindow
884-
End Function
885-
#End Region
886-
887897
#Region "CreateLWParam()"
888898
''' <summary>
889899
''' Creates a wParam or lParam value.

0 commit comments

Comments
 (0)