add debugging to profilefilter & window class
This commit is contained in:
parent
a767b9bcc0
commit
6b31549a30
@ -36,15 +36,22 @@ Public Class ProfileFilter
|
||||
|
||||
Public Sub New(LogConfig As LogConfig, ProfileDatatable As DataTable, ProcessTable As DataTable, WindowDatatable As DataTable, ControlDatatable As DataTable, TreeView As TreeView)
|
||||
Try
|
||||
_ProfileMatch = New ProfileMatch(LogConfig)
|
||||
_LogConfig = LogConfig
|
||||
_Logger = LogConfig.GetLogger()
|
||||
|
||||
_Logger.Debug("Initializing Profile Filter")
|
||||
_Logger.Debug("Initializing Profile Data")
|
||||
_ProfileTable = ProfileDatatable
|
||||
_ProcessTable = ProcessTable
|
||||
_WindowTable = WindowDatatable
|
||||
_ControlTable = ControlDatatable
|
||||
_Profiles = TransformProfiles()
|
||||
|
||||
_Logger.Debug("Initializing Profile Debugging")
|
||||
_TreeView = TreeView
|
||||
_LogConfig = LogConfig
|
||||
_Logger = LogConfig.GetLogger()
|
||||
_ProfileMatch = New ProfileMatch(LogConfig)
|
||||
|
||||
_Logger.Debug("Transforming Profiles")
|
||||
_Profiles = TransformProfiles()
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
@ -209,19 +216,19 @@ Public Class ProfileFilter
|
||||
|
||||
For Each oItem As KeyValuePair(Of String, Window.RectangleInfo) In oControlBounds
|
||||
Select Case oItem.Key
|
||||
Case "TOPLEFT"
|
||||
Case "TopLeft"
|
||||
If oControl.TopLeft.Top = oItem.Value.Top And oControl.TopLeft.Left = oItem.Value.Left Then
|
||||
oFound = True
|
||||
End If
|
||||
Case "TOPRIGHT"
|
||||
Case "TopRight"
|
||||
If oControl.TopRight.Top = oItem.Value.Top And oControl.TopLeft.Right = oItem.Value.Right Then
|
||||
oFound = True
|
||||
End If
|
||||
Case "BOTTOMLEFT"
|
||||
Case "BottomLeft"
|
||||
If oControl.BottomLeft.Bottom = oItem.Value.Bottom And oControl.TopLeft.Left = oItem.Value.Left Then
|
||||
oFound = True
|
||||
End If
|
||||
Case "BOTTOMRIGHT"
|
||||
Case "BottomRight"
|
||||
If oControl.BottomRight.Bottom = oItem.Value.Bottom And oControl.TopLeft.Right = oItem.Value.Right Then
|
||||
oFound = True
|
||||
End If
|
||||
@ -385,6 +392,7 @@ Public Class ProfileFilter
|
||||
End Function
|
||||
|
||||
Private Function TransformProfiles() As List(Of ProfileData)
|
||||
Try
|
||||
Dim oList As New List(Of ProfileData)
|
||||
|
||||
For Each oRow As DataRow In _ProfileTable.Rows
|
||||
@ -410,9 +418,14 @@ Public Class ProfileFilter
|
||||
ToList()
|
||||
|
||||
Return oList
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function TransformControls(ProfileId As Integer, ControlDatatable As DataTable) As List(Of ControlData)
|
||||
Try
|
||||
Dim oControlList As New List(Of ControlData)
|
||||
|
||||
For Each oRow As DataRow In ControlDatatable.Rows
|
||||
@ -450,8 +463,13 @@ Public Class ProfileFilter
|
||||
Next
|
||||
|
||||
Return oControlList
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
Private Function TransformProcesses(ProfileId As Integer, ProcessDatatable As DataTable) As List(Of ProcessData)
|
||||
Try
|
||||
Dim oProcessList As New List(Of ProcessData)
|
||||
|
||||
For Each oRow As DataRow In ProcessDatatable.Rows
|
||||
@ -469,8 +487,13 @@ Public Class ProfileFilter
|
||||
ToList()
|
||||
|
||||
Return oProcessList
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
Private Function TransformWindows(ProfileId As Integer, WindowDatatable As DataTable) As List(Of WindowData)
|
||||
Try
|
||||
Dim oWindowList As New List(Of WindowData)
|
||||
|
||||
For Each oRow As DataRow In WindowDatatable.Rows
|
||||
@ -486,5 +509,9 @@ Public Class ProfileFilter
|
||||
Next
|
||||
|
||||
Return oWindowList
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
|
||||
@ -127,6 +127,10 @@ Public Class NativeMethods
|
||||
Public Top As Integer
|
||||
Public Right As Integer
|
||||
Public Bottom As Integer
|
||||
|
||||
Public Overrides Function ToString() As String
|
||||
Return String.Format("Top: {0}, Bottom: {1}, Left: {2}, Right: {3}", Top, Bottom, Left, Right)
|
||||
End Function
|
||||
End Structure
|
||||
|
||||
<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)>
|
||||
|
||||
@ -31,7 +31,7 @@ Public Class Window
|
||||
Public ClassName As String = ""
|
||||
Public ProcessId As Integer = 0
|
||||
Public ControlName As String = ""
|
||||
Public hWnd As IntPtr
|
||||
Public hWnd As IntPtr = IntPtr.Zero
|
||||
End Class
|
||||
|
||||
Public Sub New(LogConfig As LogConfig)
|
||||
@ -42,23 +42,30 @@ Public Class Window
|
||||
''' Returns Information about the currently focused window
|
||||
''' </summary>
|
||||
Public Function GetWindowInfo() As WindowInfo
|
||||
Try
|
||||
Dim hWnd As IntPtr = NativeMethods.GetForegroundWindow()
|
||||
If hWnd = IntPtr.Zero Then
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Return GetWindowInfo(hWnd)
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetWindowInfo/0")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Returns Information about the Window with `hWnd`
|
||||
''' </summary>
|
||||
Public Function GetWindowInfo(ByVal hWnd As IntPtr) As WindowInfo
|
||||
Try
|
||||
_Logger.Debug("Getting Window Info")
|
||||
|
||||
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)
|
||||
|
||||
_Logger.Debug("Getting Window Text and PID")
|
||||
|
||||
NativeMethods.GetWindowText(hWnd, oWindowTitle, oTitleLength + 1)
|
||||
NativeMethods.GetWindowThreadProcessId(hWnd, oPID)
|
||||
|
||||
@ -72,6 +79,8 @@ Public Class Window
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
_Logger.Debug("Getting Class Name")
|
||||
|
||||
NativeMethods.GetClassName(hWnd, oClassBuilder, 64)
|
||||
|
||||
Return New WindowInfo With {
|
||||
@ -81,6 +90,11 @@ Public Class Window
|
||||
.ProcessName = oProcess.ProcessName,
|
||||
.WindowTitle = oWindowTitle.Replace(vbNullChar, String.Empty)
|
||||
}
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetWindowInfo/1")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
@ -92,6 +106,7 @@ Public Class Window
|
||||
Dim oWindow = GetWindowInfo()
|
||||
|
||||
If oWindow Is Nothing Then
|
||||
_Logger.Debug("Could not get Window Info!")
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
@ -104,12 +119,14 @@ Public Class Window
|
||||
Dim oControl As WindowInfo = GetWindowInfo(oControlhWnd)
|
||||
|
||||
If oControl Is Nothing Then
|
||||
_Logger.Debug("Could not get Control Info!")
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Dim oName = Utils.GetWinFormsId(oControlhWnd)
|
||||
oControl.ControlName = oName
|
||||
|
||||
|
||||
Return oControl
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
@ -120,10 +137,10 @@ Public Class Window
|
||||
|
||||
Return Nothing
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetFocusedControl/1")
|
||||
_Logger.Error(ex)
|
||||
Return Nothing
|
||||
Throw ex
|
||||
End Try
|
||||
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
@ -134,11 +151,15 @@ Public Class Window
|
||||
Dim oControlRect As New NativeMethods.RectangleAPI
|
||||
Dim oResult As New RectangleInfo
|
||||
|
||||
_Logger.Debug("Getting Control Location")
|
||||
|
||||
Try
|
||||
_Logger.Debug("Trying to get Window Rectangle")
|
||||
If NativeMethods.GetWindowRect(New HandleRef(Me, WindowHandle), oWindowRect) = False Then
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
_Logger.Debug("Trying to get Control Rectangle")
|
||||
If NativeMethods.GetWindowRect(New HandleRef(Me, ControlHandle), oControlRect) = False Then
|
||||
Return Nothing
|
||||
End If
|
||||
@ -148,6 +169,8 @@ Public Class Window
|
||||
' Calculate Coordinates relative to parent window
|
||||
oRect = GetRelativeRectangle(oControlRect, oWindowRect, Anchor)
|
||||
|
||||
_Logger.Debug("Control Location for Anchor {0}: {1}", Anchor, oRect)
|
||||
|
||||
oResult.Left = oRect.Left
|
||||
oResult.Right = oRect.Right
|
||||
oResult.Top = oRect.Top
|
||||
@ -155,8 +178,49 @@ Public Class Window
|
||||
|
||||
Return oResult
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetControlLocation/3")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function GetControlLocations(ControlHandle As IntPtr, WindowHandle As IntPtr) As Dictionary(Of String, RectangleInfo)
|
||||
Dim oWindowRect As New NativeMethods.RectangleAPI
|
||||
Dim oControlRect As New NativeMethods.RectangleAPI
|
||||
Dim oResults As New Dictionary(Of String, RectangleInfo)
|
||||
|
||||
_Logger.Debug("Getting Control Locations")
|
||||
|
||||
Try
|
||||
_Logger.Debug("Trying to get Window Rectangle")
|
||||
If NativeMethods.GetWindowRect(New HandleRef(Me, WindowHandle), oWindowRect) = False Then
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
_Logger.Debug("Trying to get Control Rectangle")
|
||||
If NativeMethods.GetWindowRect(New HandleRef(Me, ControlHandle), oControlRect) = False Then
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
|
||||
For Each oAnchor As Anchor In [Enum].GetValues(GetType(Anchor))
|
||||
Dim oRect As NativeMethods.RectangleAPI = GetRelativeRectangle(oControlRect, oWindowRect, oAnchor)
|
||||
|
||||
_Logger.Debug("Control Location for Anchor {0}: {1}", oAnchor, oRect)
|
||||
|
||||
oResults.Add(oAnchor.ToString, New RectangleInfo() With {
|
||||
.Left = oRect.Left,
|
||||
.Right = oRect.Right,
|
||||
.Top = oRect.Top,
|
||||
.Bottom = oRect.Bottom
|
||||
})
|
||||
Next
|
||||
|
||||
Return oResults
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetControlLocations/2")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
@ -164,44 +228,55 @@ Public Class Window
|
||||
''' Returns Bounds of the focused control. Relative to current form and `Anchor` value.
|
||||
''' </summary>
|
||||
Public Function GetFocusedControlLocation(WindowHandle As IntPtr, Anchor As Anchor) As RectangleInfo
|
||||
Dim oWindowRect As New NativeMethods.RectangleAPI
|
||||
Dim oControlRect As New NativeMethods.RectangleAPI
|
||||
Dim oForegroundWindow As WindowInfo
|
||||
Dim oFocusedControl As WindowInfo
|
||||
Dim oResult As New RectangleInfo
|
||||
|
||||
Try
|
||||
oForegroundWindow = GetWindowInfo()
|
||||
oFocusedControl = GetFocusedControl(WindowHandle)
|
||||
|
||||
If IsNothing(oForegroundWindow) Then
|
||||
_Logger.Debug("Foreground Window is nothing!")
|
||||
End If
|
||||
|
||||
If IsNothing(oFocusedControl) Then
|
||||
_Logger.Debug("Focused Contol is nothing!")
|
||||
End If
|
||||
|
||||
_Logger.Debug("Control Handle: {0}", oFocusedControl.hWnd)
|
||||
_Logger.Debug("Window Handle: {0}", oForegroundWindow.hWnd)
|
||||
|
||||
Return GetControlLocation(oFocusedControl.hWnd, oForegroundWindow.hWnd, Anchor)
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetFocusedControlLocation/2")
|
||||
_Logger.Error(ex)
|
||||
Return Nothing
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function GetFocusedControlLocation(WindowHandle As IntPtr) As Dictionary(Of String, RectangleInfo)
|
||||
Dim oWindowRect As New NativeMethods.RectangleAPI
|
||||
Dim oControlRect As New NativeMethods.RectangleAPI
|
||||
Dim oForegroundWindow As WindowInfo
|
||||
Dim oFocusedControl As WindowInfo
|
||||
Dim oResult As New RectangleInfo
|
||||
|
||||
Try
|
||||
oForegroundWindow = GetWindowInfo()
|
||||
oFocusedControl = GetFocusedControl(WindowHandle)
|
||||
|
||||
Dim oDict As New Dictionary(Of String, RectangleInfo)
|
||||
If oForegroundWindow Is Nothing Then
|
||||
_Logger.Warn("Foreground Window is Nothing!")
|
||||
End If
|
||||
|
||||
For Each oAnchor As Anchor In [Enum].GetValues(GetType(Anchor))
|
||||
oDict.Add(oAnchor.ToString.ToUpper, GetControlLocation(oFocusedControl.hWnd, oForegroundWindow.hWnd, oAnchor))
|
||||
Next
|
||||
If oFocusedControl Is Nothing Then
|
||||
_Logger.Warn("Focused Control is Nothing!")
|
||||
End If
|
||||
|
||||
Dim oDict As Dictionary(Of String, RectangleInfo) = GetControlLocations(oFocusedControl.hWnd, oForegroundWindow.hWnd)
|
||||
|
||||
Return oDict
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetFocusedControlLocation/1")
|
||||
_Logger.Error(ex)
|
||||
Return Nothing
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
@ -226,12 +301,14 @@ Public Class Window
|
||||
|
||||
Return GetControlLocation(oControlUnderCursor, oForegroundWindow, Anchor)
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetHoveredControlLocation/1")
|
||||
_Logger.Error(ex)
|
||||
Return Nothing
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function GetWindowRect(Handle As IntPtr)
|
||||
Try
|
||||
Dim oWindowRect As New NativeMethods.RectangleAPI
|
||||
|
||||
If NativeMethods.GetWindowRect(New HandleRef(Me, Handle), oWindowRect) = False Then
|
||||
@ -239,12 +316,20 @@ Public Class Window
|
||||
End If
|
||||
|
||||
Return oWindowRect
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetWindowRect/1")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetRelativeRectangle(ControlRect As NativeMethods.RectangleAPI, WindowRect As NativeMethods.RectangleAPI, Anchor As Anchor) As NativeMethods.RectangleAPI
|
||||
Try
|
||||
Dim oScreenRect As Rectangle = Screen.PrimaryScreen.Bounds
|
||||
Dim oLeft, oBottom, oTop, oRight As Integer
|
||||
|
||||
_Logger.Debug("Calculating Rectangle for Anchor {0}", Anchor.ToString)
|
||||
|
||||
Select Case Anchor
|
||||
Case Anchor.TopLeft
|
||||
oLeft = ControlRect.Left - WindowRect.Left
|
||||
@ -260,15 +345,23 @@ Public Class Window
|
||||
oBottom = ControlRect.Bottom - WindowRect.Bottom
|
||||
End Select
|
||||
|
||||
_Logger.Debug("Done Calculating Rectangle for Anchor {0}", Anchor.ToString)
|
||||
|
||||
Return New NativeMethods.RectangleAPI() With {
|
||||
.Top = oTop,
|
||||
.Bottom = oBottom,
|
||||
.Left = oLeft,
|
||||
.Right = oRight
|
||||
}
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetRelativeRectangle/3")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetRect(Rect As NativeMethods.RectangleAPI, ParentRect As NativeMethods.RectangleAPI) As Rectangle
|
||||
Try
|
||||
Dim oX, oY, oWidth, oHeight As Integer
|
||||
|
||||
oWidth = Rect.Right - Rect.Left
|
||||
@ -277,9 +370,15 @@ Public Class Window
|
||||
oY = Rect.Top - ParentRect.Top
|
||||
|
||||
Return New Rectangle(oX, oY, oWidth, oHeight)
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetRect/2")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetRect(Rect As NativeMethods.RectangleAPI, ParentRect As Rectangle) As Rectangle
|
||||
Try
|
||||
Dim oX, oY, oWidth, oHeight As Integer
|
||||
|
||||
oWidth = Rect.Right - Rect.Left
|
||||
@ -288,5 +387,10 @@ Public Class Window
|
||||
oY = Rect.Top - ParentRect.Y
|
||||
|
||||
Return New Rectangle(oX, oY, oWidth, oHeight)
|
||||
Catch ex As Exception
|
||||
_Logger.Debug("Error in GetRect/2")
|
||||
_Logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user