Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
smoorke committed Mar 26, 2024
1 parent c3a94df commit a47b598
Show file tree
Hide file tree
Showing 19 changed files with 2,132 additions and 0 deletions.
Binary file added 008555f112ea2f0383af3142cfffe8f21ffb961e.ico
Binary file not shown.
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
128 changes: 128 additions & 0 deletions MouseHook.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@

Imports System.Runtime.InteropServices

Public Class MouseHook : Implements IDisposable



Private Const HC_ACTION As Integer = 0
Private Const WH_MOUSE_LL As Integer = 14
Private Const WM_MOUSEMOVE As Integer = &H200

Public Delegate Function MouseHookCallBack(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As Integer

<DllImport("Kernel32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function GetModuleHandle(ByVal ModuleName As String) As IntPtr : End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function SetWindowsHookEx(idHook As Integer, HookProc As MouseHookCallBack,
hInstance As IntPtr, ThreadId As Integer) As IntPtr : End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function CallNextHookEx(hHook As IntPtr, nCode As Integer,
wParam As IntPtr, lParam As IntPtr) As Integer : End Function

<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function UnhookWindowsHookEx(hHook As IntPtr) As Boolean : End Function

Public hwnd As IntPtr = IntPtr.Zero
Friend rcC As RECT
Private Shared HookHandle As IntPtr = IntPtr.Zero

Private Function MouseProc(
ByVal nCode As Integer,
ByVal wParam As IntPtr,
ByVal lParam As IntPtr) As Integer

If (nCode = HC_ACTION) Then
Select Case wParam.ToInt32()
Case WM_MOUSEMOVE

If hwnd = IntPtr.Zero Then Exit Select
If hwnd <> GetForegroundWindow() Then Exit Select 'GetForegroundWindow() can be IntPtr.Zero when switching active app

Dim pci As New CURSORINFO With {.cbSize = Marshal.SizeOf(GetType(CURSORINFO))}
GetCursorInfo(pci)
If pci.flags <> 0 Then Exit Select 'Cursor is visible

'We only care about the first member so we marshal directly to a point
Dim cpos As Point = Marshal.PtrToStructure(Of Point)(lParam) 'cursor position

'top left corner
Dim ptCTL = New Point(0, 0)
ClientToScreen(hwnd, ptCTL)

'bottom right corner
Dim ptCBR = New Point(rcC.right, rcC.bottom)
ClientToScreen(hwnd, ptCBR)

If cpos.X < ptCTL.X AndAlso cpos.Y < ptCTL.Y Then 'top left corner
Cursor.Position = ptCTL 'New Point(ptCTL.X, ptCTL.Y)
Return 1
ElseIf cpos.X > ptCBR.X AndAlso cpos.Y < ptCTL.Y Then 'top right corner
Cursor.Position = New Point(ptCBR.X, ptCTL.Y)
Return 1
ElseIf cpos.X > ptCBR.X AndAlso cpos.Y > ptCBR.Y Then 'bottom right corner
Cursor.Position = ptCBR 'New Point(ptCBR.X, ptCBR.Y)
Return 1
ElseIf cpos.X < ptCTL.X AndAlso cpos.Y > ptCBR.Y Then 'bottom left corner
Cursor.Position = New Point(ptCTL.X, ptCBR.Y)
Return 1
ElseIf cpos.X < ptCTL.X Then 'left border
Cursor.Position = New Point(ptCTL.X, cpos.Y)
Return 1
ElseIf cpos.X > ptCBR.X Then 'right border
Cursor.Position = New Point(ptCBR.X, cpos.Y)
Return 1
ElseIf cpos.Y > ptCBR.Y Then 'bottom border
Cursor.Position = New Point(cpos.X, ptCBR.Y)
Return 1
ElseIf cpos.Y < ptCTL.Y Then 'top border
Cursor.Position = New Point(cpos.X, ptCTL.Y)
Return 1
End If
End Select
End If

Return CallNextHookEx(HookHandle, nCode, wParam, lParam)

End Function
Private mhCallBack As MouseHookCallBack = New MouseHookCallBack(AddressOf MouseProc)
Private disposedValue As Boolean

Public Sub HookMouse()
HookHandle = SetWindowsHookEx(WH_MOUSE_LL, mhCallBack,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0)
If HookHandle = IntPtr.Zero Then Throw New System.Exception("Mouse hook failed")
End Sub

Public Sub UnhookMouse()
If HookHandle <> IntPtr.Zero Then UnhookWindowsHookEx(HookHandle)
End Sub

Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects)
End If

' TODO: free unmanaged resources (unmanaged objects) and override finalizer
UnhookMouse()
' TODO: set large fields to null
disposedValue = True
End If
End Sub

' TODO: override finalizer only if 'Dispose(disposing As Boolean)' has code to free unmanaged resources
Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
Dispose(disposing:=False)
MyBase.Finalize()
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in 'Dispose(disposing As Boolean)' method
Dispose(disposing:=True)
GC.SuppressFinalize(Me)
End Sub
End Class
44 changes: 44 additions & 0 deletions My Project/Application.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions My Project/Application.myapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<MyApplicationData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MySubMain>true</MySubMain>
<MainForm>frmMain</MainForm>
<SingleInstance>false</SingleInstance>
<ShutdownMode>0</ShutdownMode>
<EnableVisualStyles>true</EnableVisualStyles>
<AuthenticationMode>0</AuthenticationMode>
<MinimumSplashScreenDisplayTime>0</MinimumSplashScreenDisplayTime>
<SaveMySettingsOnExit>true</SaveMySettingsOnExit>
<HighDpiMpde>false</HighDpiMpde>
</MyApplicationData>
35 changes: 35 additions & 0 deletions My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

' General Information about an assembly is controlled through the following
' set of attributes. Change these attribute values to modify the information
' associated with an assembly.

' Review the values of the assembly attributes

<Assembly: AssemblyTitle("WolfClipper")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("WolfClipper")>
<Assembly: AssemblyCopyright("Copyright © 2024")>
<Assembly: AssemblyTrademark("")>

<Assembly: ComVisible(False)>

'The following GUID is for the ID of the typelib if this project is exposed to COM
<Assembly: Guid("37d56f2d-e5e7-49ee-9f91-c3755324f038")>

' Version information for an assembly consists of the following four values:
'
' Major Version
' Minor Version
' Build Number
' Revision
'
' You can specify all the values or you can default the Build and Revision Numbers
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("1.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>
63 changes: 63 additions & 0 deletions My Project/Resources.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a47b598

Please sign in to comment.