Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Text
Imports System.Text.Encoding
Namespace Enumeration
Public Class Window
#Region " Declares "
<DllImport("user32.dll", EntryPoint:="GetWindowText")> _
Private Shared Sub GetWindowText _
(ByVal hWnd As IntPtr, ByVal sbText As StringBuilder, ByVal iLength As Integer)
End Sub
<DllImport("user32.dll", EntryPoint:="IsWindowVisible")> _
Private Shared Function IsWindowVisible _
(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="IsIconic")> _
Private Shared Function IsIconic _
(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect _
(ByVal hWnd As IntPtr, ByRef rectBounds As RECT) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
Public Sub New(ByVal pLeft As Integer, ByVal pTop As Integer, ByVal pRight As Integer, ByVal pBottom As Integer)
Left = pLeft
Top = pTop
Right = pRight
Bottom = pBottom
End Sub
Public ReadOnly Property Height() As Integer
Get
Return Bottom - Top
End Get
End Property
Public ReadOnly Property Width() As Integer
Get
Return Right - Left
End Get
End Property
Public ReadOnly Property Location() As Point
Get
Return New Point(Left, Top)
End Get
End Property
Public ReadOnly Property Size() As Size
Get
Return New Size(Width, Height)
End Get
End Property
Public Function ToRectangle() As Rectangle
Return Rectangle.FromLTRB(Me.Left, Me.Top, Me.Right, Me.Bottom)
End Function
Public Shared Function FromRectangle(ByVal r As Rectangle) As RECT
Return New RECT(r.Left, r.Top, r.Right, r.Bottom)
End Function
End Structure
<DllImport("user32.dll")> _
Private Shared Function PrintWindow _
(ByVal hWnd As IntPtr, ByVal hDC As IntPtr, ByVal iFlags As UInteger) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function ShowWindow _
(ByVal hWnd As IntPtr, ByVal iFlags As ShowWindowEnum) As Boolean
End Function
Private Enum ShowWindowEnum
Hide = 0
ShowNormal = 1
ShowMinimized = 2
ShowMaximized = 3
Maximize = 3
ShowNormalNoActivate = 4
Show = 5
Minimize = 6
ShowMinNoActivate = 7
ShowNoActivate = 8
Restore = 9
ShowDefault = 10
ForceMinimized = 11
End Enum
<DllImport("user32.dll")> _
Private Shared Function SendMessage _
(ByVal hWnd As IntPtr, ByVal iMsg As UInteger, ByVal wParam As UInteger, ByVal lParam As UInteger) As UInteger
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowLong _
(ByVal hWnd As IntPtr, ByVal iIndex As Integer) As Integer
End Function
<DllImport("user32.dll")> _
Private Shared Function SetWindowLong _
(ByVal hWnd As IntPtr, ByVal iIndex As Integer, ByVal iNewLong As Integer) As Integer
End Function
<DllImport("user32.dll")> _
Private Shared Function SetLayeredWindowAttributes _
(ByVal hWnd As IntPtr, ByVal bytCrey As Byte, ByVal bytAlpha As Byte, ByVal iFlags As Integer) As Integer
End Function
Public Const WM_PAINT = &HF
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const LWA_ALPHA = &H2
#End Region
Private mhWnd As IntPtr
Sub New(ByVal hWnd As IntPtr)
mhWnd = hWnd
End Sub
Public ReadOnly Property Handle() As IntPtr
Get
Return mhWnd
End Get
End Property
Public ReadOnly Property Title() As String
Get
Dim sbTitle As New StringBuilder(1024)
GetWindowText(mhWnd, sbTitle, sbTitle.Capacity)
Return sbTitle.ToString
End Get
End Property
Public ReadOnly Property Visible() As Boolean
Get
Return IsWindowVisible(mhWnd)
End Get
End Property
Public ReadOnly Property Minimized() As Boolean
Get
Return IsIconic(mhWnd)
End Get
End Property
Public ReadOnly Property Bounds() As Rectangle
Get
Dim rectBounds As New RECT
GetWindowRect(mhWnd, rectBounds)
Return rectBounds.ToRectangle
End Get
End Property
Public ReadOnly Property Mentionable() As Boolean
Get
If Title.Length > 0 And Not Title.Equals("Program Manager") And Not Title.Equals("Start") And Visible Then
Return True
Else
Return False
End If
End Get
End Property
Public ReadOnly Property Key() As String
Get
Return System.Convert.ToBase64String(UTF8.GetBytes(mhWnd.ToString))
End Get
End Property
Public Function Image() As Bitmap
Dim bSpecial As Boolean = Minimized
If (bSpecial) Then EnterSpecialCapturing()
'FIX
Dim bmp As Bitmap
Dim rctBounds As Rectangle = Bounds
Try
If (rctBounds.IsEmpty Or rctBounds.Height < 0 Or rctBounds.Width < 0) Then bmp = Nothing
bmp = New Bitmap(rctBounds.Width, rctBounds.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim dc As IntPtr = g.GetHdc()
PrintWindow(mhWnd, dc, 0)
g.ReleaseHdc()
g.Dispose()
Catch
bmp = Nothing
End Try
'FIX
If (bSpecial) Then ExitSpecialCapturing(mhWnd)
End Function
End Namespace