0

この C# コード(自分のものではない! ) をVB.NETで動作させ、コンソール アプリケーションにマウス フックを実装することができました。VB.NET コードは次のとおりです。

Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Module Module1
Class InterceptMouse
    Private Shared _proc As LowLevelMouseProc = AddressOf HookCallback
    Private Shared _hookID As IntPtr = IntPtr.Zero

    Public Shared Sub Main()
        _hookID = SetHook(_proc)
        Application.Run()
        UnhookWindowsHookEx(_hookID)
    End Sub

    Private Shared Function SetHook(proc As LowLevelMouseProc) As IntPtr
        Using curProcess As Process = Process.GetCurrentProcess()
            Using curModule As ProcessModule = curProcess.MainModule
                Return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
            End Using
        End Using
    End Function

    Private Delegate Function LowLevelMouseProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr

    Private Shared Function HookCallback(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
        Dim MouseSetting As Integer = MouseMessages.WM_LBUTTONDOWN
        Dim hookStruct As MSLLHOOKSTRUCT
        If nCode >= 0 AndAlso MouseSetting = CType(wParam, MouseMessages) Then
            hookStruct = CType(Marshal.PtrToStructure(lParam, GetType(MSLLHOOKSTRUCT)), MSLLHOOKSTRUCT)
            Console.WriteLine(hookStruct.pt.x & ", " & hookStruct.pt.y)
        End If
        Dim pt As New POINT
        pt.x = hookStruct.pt.x
        pt.y = hookStruct.pt.y
        MouseCoordinates.Add(pt)
        Return CallNextHookEx(_hookID, nCode, wParam, lParam)

    End Function

    Private Const WH_MOUSE_LL As Integer = 14

    Private Enum MouseMessages
        WM_LBUTTONDOWN = &H201
        WM_LBUTTONUP = &H202
        WM_MOUSEMOVE = &H200
        WM_MOUSEWHEEL = &H20A
        WM_RBUTTONDOWN = &H204
        WM_RBUTTONUP = &H205
    End Enum

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure POINT
        Public x As Integer
        Public y As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure MSLLHOOKSTRUCT
        Public pt As POINT
        Public mouseData As UInteger
        Public flags As UInteger
        Public time As UInteger
        Public dwExtraInfo As IntPtr
    End Structure

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function SetWindowsHookEx(idHook As Integer, lpfn As LowLevelMouseProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function UnhookWindowsHookEx(hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
    End Function
End Class

次に、サブメインから呼び出しました。

Sub Main()
    InterceptMouse.Main()
End Sub

しかし、デバッグすると Application.Run() の行まで行って次の行に進まないので、裏で何をしているのかわかりません。次に、ユーザーの入力を待ち、コンソールに整数の座標を返します。これは私がやりたいことではありません。このコードを別の関数またはバックグラウンド ワーカーで実行し、マウス クリックを受け取るたびに座標をメイン スレッドに渡して、残りを処理できるようにしたいと考えています。問題は、このコードが私の理解レベルには複雑すぎて、どのように機能するのかよくわからないことです。ユーザーがクリックするたびにコンソールがマウス座標を返すように少なくとも管理できれば、それを無視してそこから残りのコードを処理できるはずです。

4

1 に答える 1