add Modules.Windows

This commit is contained in:
Jonathan Jenne 2019-09-18 12:09:21 +02:00
parent 8a8b286c77
commit 74527690cd
4 changed files with 233 additions and 16 deletions

27
Windows/Drawing.vb Normal file
View File

@ -0,0 +1,27 @@
Imports System.Drawing
Imports DigitalData.Modules.Logging
Public Class Drawing
Private _Logger As Logger
Public Sub New(LogConfig As LogConfig)
_Logger = LogConfig.GetLogger()
End Sub
Public Sub DrawRectangle(Bounds As Rectangle)
Dim oContext As IntPtr
oContext = NativeMethods.GetDC(IntPtr.Zero)
Try
Dim g As Graphics
g = Graphics.FromHdc(oContext)
Try
g.DrawRectangle(Pens.Red, Bounds)
Finally
g.Dispose()
End Try
Finally
NativeMethods.ReleaseDC(IntPtr.Zero, oContext)
End Try
End Sub
End Class

View File

@ -2,6 +2,15 @@
Imports System.Text Imports System.Text
Public Class NativeMethods Public Class NativeMethods
<DllImport("user32.dll")>
Public Shared Function GetDC(ByVal hwnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")>
Public Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")>
Public Shared Function GetWindowRect(ByVal hWnd As HandleRef, ByRef lpRect As RectangleAPI) As Boolean
End Function
<DllImport("user32.dll")> <DllImport("user32.dll")>
Public Shared Function AttachThreadInput(ByVal idAttach As IntPtr, ByVal idAttachTo As IntPtr, fAttach As Boolean) As Boolean Public Shared Function AttachThreadInput(ByVal idAttach As IntPtr, ByVal idAttachTo As IntPtr, fAttach As Boolean) As Boolean
End Function End Function
@ -9,6 +18,9 @@ Public Class NativeMethods
Public Shared Function GetFocus() As IntPtr Public Shared Function GetFocus() As IntPtr
End Function End Function
<DllImport("user32.dll")> <DllImport("user32.dll")>
Public Shared Function WindowFromPoint(ByVal p As PointAPI) As IntPtr
End Function
<DllImport("user32.dll")>
Public Shared Function GetForegroundWindow() As IntPtr Public Shared Function GetForegroundWindow() As IntPtr
End Function End Function
<DllImport("user32.dll")> <DllImport("user32.dll")>
@ -63,6 +75,9 @@ Public Class NativeMethods
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function RegisterWindowMessage(ByVal lpString As String) As Integer Public Shared Function RegisterWindowMessage(ByVal lpString As String) As Integer
End Function End Function
<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)>
Public Shared Function GetCursorPos(ByRef lpPoint As PointAPI) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000 Public Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Public Const SECTION_QUERY As Short = &H1 Public Const SECTION_QUERY As Short = &H1
@ -99,4 +114,29 @@ Public Class NativeMethods
NoCache = &H200 NoCache = &H200
WriteCombine = &H400 WriteCombine = &H400
End Enum End Enum
Public Enum ChildWindowFromPointFlags As UInteger
CWP_ALL
CWP_SKIPINVISIBLE
CWP_SKIPDISABLED
CWP_SKIPTRANSPARENT
End Enum
Public Structure RectangleAPI
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)>
Public Structure PointAPI
Public X As Integer
Public Y As Integer
Public Sub New(ByVal X As Integer, ByVal Y As Integer)
Me.X = X
Me.Y = Y
End Sub
End Structure
End Class End Class

View File

@ -1,9 +1,30 @@
Imports System.Text Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Windows.Forms
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Public Class Window Public Class Window
Private _Logger As Logger Private _Logger As Logger
Public Enum Anchor
TopLeft
BottomLeft
TopRight
BottomRight
End Enum
Public Class RectangleInfo
Public Top As Integer = 0
Public Left As Integer = 0
Public Right As Integer = 0
Public Bottom As Integer = 0
Public Overrides Function ToString() As String
Return String.Format("Top:{0},Left:{1},Bottom:{2},Right:{3}", Top, Left, Bottom, Right)
End Function
End Class
Public Class WindowInfo Public Class WindowInfo
Public WindowTitle As String = "" Public WindowTitle As String = ""
Public ProcessName As String = "" Public ProcessName As String = ""
@ -14,7 +35,7 @@ Public Class Window
End Class End Class
Public Sub New(LogConfig As LogConfig) Public Sub New(LogConfig As LogConfig)
_Logger = LogConfig.getLogger() _Logger = LogConfig.GetLogger()
End Sub End Sub
''' <summary> ''' <summary>
@ -62,20 +83,6 @@ Public Class Window
} }
End Function 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> ''' <summary>
''' Returns the currently focused control ''' Returns the currently focused control
''' </summary> ''' </summary>
@ -118,4 +125,144 @@ Public Class Window
End Try End Try
End Function End Function
''' <summary>
''' Returns Bounds of `ControlHandle`. Relative to `WindowHandle` and `Anchor` value.
''' </summary>
Public Function GetControlLocation(ControlHandle As IntPtr, WindowHandle As IntPtr, Optional Anchor As Anchor = Anchor.TopLeft) As RectangleInfo
Dim oWindowRect As New NativeMethods.RectangleAPI
Dim oControlRect As New NativeMethods.RectangleAPI
Dim oResult As New RectangleInfo
Try
If NativeMethods.GetWindowRect(New HandleRef(Me, WindowHandle), oWindowRect) = False Then
Return Nothing
End If
If NativeMethods.GetWindowRect(New HandleRef(Me, ControlHandle), oControlRect) = False Then
Return Nothing
End If
Dim oRect As New NativeMethods.RectangleAPI
' Calculate Coordinates relative to parent window
oRect = GetRelativeRectangle(oControlRect, oWindowRect, Anchor)
oResult.Left = oRect.Left
oResult.Right = oRect.Right
oResult.Top = oRect.Top
oResult.Bottom = oRect.Bottom
Return oResult
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
''' <summary>
''' Returns Bounds of the focused control. Relative to current form and `Anchor` value.
''' </summary>
Public Function GetFocusedControlLocation(Optional Anchor As Anchor = Anchor.TopLeft) As RectangleInfo
Dim oWindowRect As New NativeMethods.RectangleAPI
Dim oControlRect As New NativeMethods.RectangleAPI
Dim oForegroundWindow As IntPtr
Dim oFocusedControl As IntPtr
Dim oResult As New RectangleInfo
Try
oForegroundWindow = NativeMethods.GetForegroundWindow()
oFocusedControl = NativeMethods.GetFocus()
Return GetControlLocation(oFocusedControl, oForegroundWindow, Anchor)
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
''' <summary>
''' Returns Bounds of the control under the cursor. Relative to current form and `Anchor` value.
''' </summary>
Public Function GetHoveredControlLocation(Optional Anchor As Anchor = Anchor.TopLeft) As RectangleInfo
Dim oPoint As New NativeMethods.PointAPI
Dim oWindowRect As New NativeMethods.RectangleAPI
Dim oControlRect As New NativeMethods.RectangleAPI
Dim oForegroundWindow As IntPtr
Dim oControlUnderCursor As IntPtr
Dim oResult As New RectangleInfo
Try
If NativeMethods.GetCursorPos(oPoint) = False Then
Return Nothing
End If
oForegroundWindow = NativeMethods.GetForegroundWindow()
oControlUnderCursor = NativeMethods.WindowFromPoint(oPoint)
Return GetControlLocation(oControlUnderCursor, oForegroundWindow, Anchor)
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function GetWindowRect(Handle As IntPtr)
Dim oWindowRect As New NativeMethods.RectangleAPI
If NativeMethods.GetWindowRect(New HandleRef(Me, Handle), oWindowRect) = False Then
Return Nothing
End If
Return oWindowRect
End Function
Private Function GetRelativeRectangle(ControlRect As NativeMethods.RectangleAPI, WindowRect As NativeMethods.RectangleAPI, Anchor As Anchor) As NativeMethods.RectangleAPI
Dim oScreenRect As Rectangle = Screen.PrimaryScreen.Bounds
Dim oLeft, oBottom, oTop, oRight As Integer
Select Case Anchor
Case Anchor.TopLeft
oLeft = ControlRect.Left - WindowRect.Left
oTop = ControlRect.Top - WindowRect.Top
Case Anchor.BottomLeft
oLeft = ControlRect.Left - WindowRect.Left
oBottom = ControlRect.Bottom - WindowRect.Bottom
Case Anchor.TopRight
oRight = ControlRect.Right - WindowRect.Right
oTop = ControlRect.Top - WindowRect.Top
Case Anchor.BottomRight
oRight = ControlRect.Right - WindowRect.Right
oBottom = ControlRect.Bottom - WindowRect.Bottom
End Select
Return New NativeMethods.RectangleAPI() With {
.Top = oTop,
.Bottom = oBottom,
.Left = oLeft,
.Right = oRight
}
End Function
Private Function GetRect(Rect As NativeMethods.RectangleAPI, ParentRect As NativeMethods.RectangleAPI) As Rectangle
Dim oX, oY, oWidth, oHeight As Integer
oWidth = Rect.Right - Rect.Left
oHeight = Rect.Bottom - Rect.Top
oX = Rect.Left - ParentRect.Left
oY = Rect.Top - ParentRect.Top
Return New Rectangle(oX, oY, oWidth, oHeight)
End Function
Private Function GetRect(Rect As NativeMethods.RectangleAPI, ParentRect As Rectangle) As Rectangle
Dim oX, oY, oWidth, oHeight As Integer
oWidth = Rect.Right - Rect.Left
oHeight = Rect.Bottom - Rect.Top
oX = Rect.Left - ParentRect.X
oY = Rect.Top - ParentRect.Y
Return New Rectangle(oX, oY, oWidth, oHeight)
End Function
End Class End Class

View File

@ -50,10 +50,12 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" /> <Reference Include="System.IO.Compression" />
<Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" /> <Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" /> <Reference Include="System.Transactions" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@ -72,6 +74,7 @@
<Import Include="System.Threading.Tasks" /> <Import Include="System.Threading.Tasks" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Drawing.vb" />
<Compile Include="NativeMethods.vb" /> <Compile Include="NativeMethods.vb" />
<Compile Include="Utils.vb" /> <Compile Include="Utils.vb" />
<Compile Include="Window.vb" /> <Compile Include="Window.vb" />