Imports System.Text
Imports DigitalData.Modules.Logging
Public Class Window
Private _Logger As Logger
Public Class WindowInfo
Public WindowTitle As String = ""
Public ProcessName As String = ""
Public ClassName As String = ""
Public ProcessId As Integer = 0
Public ControlName As String = ""
Public hWnd As IntPtr
End Class
Public Sub New(LogConfig As LogConfig)
_Logger = LogConfig.getLogger()
End Sub
'''
''' Returns Information about the currently focused window
'''
Public Function GetWindowInfo() As WindowInfo
Dim hWnd As IntPtr = NativeMethods.GetForegroundWindow()
If hWnd = IntPtr.Zero Then
Return Nothing
End If
Return GetWindowInfo(hWnd)
End Function
'''
''' Returns Information about the Window with `hWnd`
'''
Public Function GetWindowInfo(ByVal hWnd As IntPtr) As WindowInfo
Dim oPID As Integer = 0
Dim oTitleLength As Int32 = NativeMethods.GetWindowTextLength(hWnd)
Dim oWindowTitle As String = StrDup(oTitleLength + 1, "*")
Dim oClassBuilder As New StringBuilder(64)
NativeMethods.GetWindowText(hWnd, oWindowTitle, oTitleLength + 1)
NativeMethods.GetWindowThreadProcessId(hWnd, oPID)
If oPID = 0 Then
Return Nothing
End If
Dim oProcess As Process = Process.GetProcessById(oPID)
If oProcess Is Nothing Then
Return Nothing
End If
NativeMethods.GetClassName(hWnd, oClassBuilder, 64)
Return New WindowInfo With {
.hWnd = hWnd,
.ClassName = oClassBuilder.ToString,
.ProcessId = oProcess.Id,
.ProcessName = oProcess.ProcessName,
.WindowTitle = oWindowTitle.Replace(vbNullChar, String.Empty)
}
End Function
Public Function FocusedControlinActiveWindow(WindowHandle As IntPtr) As IntPtr
Try
Dim oActiveWindowHandle As IntPtr = NativeMethods.GetForegroundWindow
Dim oActiveWindowThread As IntPtr = NativeMethods.GetWindowThreadProcessId(oActiveWindowHandle, 0)
Dim oThisWindowThread As IntPtr = NativeMethods.GetWindowThreadProcessId(WindowHandle, 0)
NativeMethods.AttachThreadInput(oActiveWindowThread, oThisWindowThread, True)
Dim oFocusedControlHandle As IntPtr = NativeMethods.GetFocus()
NativeMethods.AttachThreadInput(oActiveWindowThread, oThisWindowThread, False)
Return oFocusedControlHandle
Catch ex As Exception
Return Nothing
End Try
End Function
'''
''' Returns the currently focused control
'''
''' Current window handle; can be obtained from Me.Handle
Public Function GetFocusedControl(WindowHandle As IntPtr) As WindowInfo
Try
Dim oWindow = GetWindowInfo()
If oWindow Is Nothing Then
Return Nothing
End If
Dim oThreadId As IntPtr = NativeMethods.GetWindowThreadProcessId(oWindow.hWnd, 0)
Dim oMyThreadId As IntPtr = NativeMethods.GetWindowThreadProcessId(WindowHandle, 0)
If NativeMethods.AttachThreadInput(oThreadId, oMyThreadId, True) Then
Try
Dim oControlhWnd = NativeMethods.GetFocus()
Dim oControl As WindowInfo = GetWindowInfo(oControlhWnd)
If oControl Is Nothing Then
Return Nothing
End If
Dim oName = Utils.GetWinFormsId(oControlhWnd)
oControl.ControlName = oName
Return oControl
Catch ex As Exception
_Logger.Error(ex)
Finally
NativeMethods.AttachThreadInput(oThreadId, oMyThreadId, False)
End Try
End If
Return Nothing
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
End Class