Imports System.Runtime.InteropServices
Imports System.Net.Mail
Imports System.Text
Public Class Overlay
#Region " P/Invokes "
<DllImport("user32.dll")> _
Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As UInteger) As Integer
End Function
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As IntPtr
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As IntPtr
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
#End Region
#Region " Properties "
Private Shared Monitor As Boolean = True
Public Property ActivateMonitoring As Boolean
Get
Return Monitor
End Get
Set(value As Boolean)
Monitor = value
End Set
End Property
Private Shared GameWindowsTitle As String = String.Empty
Public ReadOnly Property GetWindowsTitle As String
Get
Return GameWindowsTitle
End Get
End Property
Private Shared GameHandle As IntPtr = Nothing
Public ReadOnly Property GetGameHandle As IntPtr
Get
Return GameHandle
End Get
End Property
Private Shared GameLocation As Point = Nothing
Public ReadOnly Property GetGameLocation As Point
Get
Return GameLocation
End Get
End Property
Private Shared GameSize As Size = Nothing
Public ReadOnly Property GetGameSize As Size
Get
Return GameSize
End Get
End Property
#End Region
#Region " Structures "
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
#End Region
#Region " Declare's "
Public window_loc As RECT
Public screencenter(1) As Integer
Public hWnd As IntPtr = Nothing
Public pHandle As IntPtr = Nothing
Public processID As Integer = Nothing
Private ProcessName As String = String.Empty
Public Const PROCESS_VM_ALL As Integer = &H1F0FFF
Private MyAppProcess As String = Process.GetCurrentProcess().ProcessName
Private FormManagement As Form = Nothing
#End Region
#Region " Public Methods "
Public Sub New(ByVal ProcName As String, Optional ByVal FormC As Form = Nothing)
ProcessName = ProcName
If ProcessName.ToLower.EndsWith(".exe") Then ProcessName = ProcessName.Substring(0, ProcessName.Length - 4)
FormManagement = FormC
Dim tsk As New Task(AddressOf AttachtClient, TaskCreationOptions.LongRunning)
tsk.Start()
End Sub
#End Region
#Region " Private Methods "
Private Sub AttachtClient()
'On Error Resume Next
Do While True
If Monitor = True Then
If Not ProcessName = String.Empty Then
Dim proc() As Process = Process.GetProcessesByName(ProcessName)
If Not proc.Length = 0 Then
If IsGameAppFocus() = True Then
Dim windowname As String = proc(0).MainWindowTitle
GameWindowsTitle = windowname
hWnd = FindWindow(vbNullString, windowname)
GetWindowThreadProcessId(hWnd, processID)
pHandle = OpenProcess(PROCESS_VM_ALL, 0, processID)
GameHandle = pHandle
If hWnd = 0 Then Exit Do
GetWindowRect(hWnd, window_loc)
GameLocation = New Point((window_loc.Left + 10), (window_loc.Top + 35))
GameSize = New Point(((window_loc.Right - window_loc.Left) - 25), ((window_loc.Bottom - window_loc.Top) - 45))
If Not (FormManagement Is Nothing) Then
If FormManagement.Visible = True Then
FormManagement.BeginInvoke(Sub()
FormManagement.Location = GameLocation
FormManagement.Size = GameSize
End Sub)
End If
End If
Else
If FormManagement.Visible = True Then
FormManagement.BeginInvoke(Sub()
FormManagement.Hide()
End Sub)
End If
End If
Else
is_active(True)
End If
End If
End If
Loop
End Sub
Private Function IsGameAppFocus() As Boolean
Dim ActiveProcess As Process = GetActiveProcess()
Dim IsFocusGame As Boolean = False
If ActiveProcess IsNot Nothing Then
Dim CurrentProcName As String = ActiveProcess.ProcessName
If LCase(CurrentProcName) = LCase(MyAppProcess) Then
IsFocusGame = True
End If
If LCase(CurrentProcName) = LCase(ProcessName) Then
IsFocusGame = True
End If
Return IsFocusGame
End If
Return IsFocusGame
End Function
Private Function GetActiveProcess() As Process
Dim FocusedWindow As IntPtr = GetForegroundWindow()
If FocusedWindow = IntPtr.Zero Then Return Nothing
Dim FocusedWindowProcessId As UInteger = 0
GetWindowThreadProcessId(FocusedWindow, FocusedWindowProcessId)
If FocusedWindowProcessId = 0 Then Return Nothing
Return Process.GetProcessById(CType(FocusedWindowProcessId, Integer))
End Function
Private Sub is_active(Optional ByVal exit_ As Boolean = True)
Dim p As Process() = Process.GetProcessesByName(ProcessName)
If p.Length = 0 And exit_ Then
Environment.Exit(0)
End If
End Sub
#End Region
End Class