monster commit for zoo flow, prepare migration of cw
This commit is contained in:
121
Windows/Window.vb
Normal file
121
Windows/Window.vb
Normal file
@@ -0,0 +1,121 @@
|
||||
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
|
||||
|
||||
''' <summary>
|
||||
''' Returns Information about the currently focused window
|
||||
''' </summary>
|
||||
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
|
||||
|
||||
''' <summary>
|
||||
''' Returns Information about the Window with `hWnd`
|
||||
''' </summary>
|
||||
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
|
||||
|
||||
''' <summary>
|
||||
''' Returns the currently focused control
|
||||
''' </summary>
|
||||
''' <param name="WindowHandle">Current window handle; can be obtained from Me.Handle</param>
|
||||
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
|
||||
Reference in New Issue
Block a user