マウスの動きを記録し、再生するソフトを vbc.exe で作成するには、Windows API を使用してマウスの位置を取得し、再生時にその位置に移動するようにします。以下に基本的なサンプルコードを示します。

ステップ 1: マウスの動きを記録する

マウスの座標を一定時間ごとに記録し、タイムスタンプと一緒に保存します。

 

Imports System.Runtime.InteropServices

Imports System.IO

Imports System.Threading

Imports System.Collections.Generic ' List(Of String) を使うためのインポート

 

Module Module1

    <StructLayout(LayoutKind.Sequential)>

    Public Structure POINT

        Public X As Integer

        Public Y As Integer

    End Structure

 

    <DllImport("user32.dll")>

    Public Function GetCursorPos(ByRef lpPoint As POINT) As Boolean

    End Function

 

    Dim mouseMovements As New List(Of String)

 

    Sub Main()

        Console.WriteLine("マウスの動きを記録中... 終了するにはEscキーを押してください")

        Dim stopRecording As Boolean = False

 

        While Not stopRecording

            Dim p As POINT

            GetCursorPos(p)

 

            ' String.Format を使用して文字列を作成

            mouseMovements.Add(String.Format("{0},{1},{2}", p.X, p.Y, DateTime.Now.Ticks))

 

            ' Escキーが押されたら記録を終了

            If Console.KeyAvailable AndAlso Console.ReadKey(True).Key = ConsoleKey.Escape Then

                stopRecording = True

            End If

 

            ' 次の座標を記録するまで少し待つ

            Thread.Sleep(100)

        End While

 

        ' 記録したマウスの動きをファイルに保存

        File.WriteAllLines("mouseMovements.txt", mouseMovements)

        Console.WriteLine("マウスの動きが保存されました")

    End Sub

End Module

 

ステップ 2: マウスの動きを再生する

記録したマウスの座標を読み込み、再生時にマウスカーソルをその座標に移動させます。

Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Threading

 

Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Threading

Module Module2
    <StructLayout(LayoutKind.Sequential)>
    Public Structure POINT
        Public X As Integer
        Public Y As Integer
    End Structure

    <DllImport("user32.dll")>
    Public Function SetCursorPos(x As Integer, y As Integer) As Boolean
    End Function

    Sub Main()
        ' 保存されたマウスの動きを読み込む
        If Not File.Exists("mouseMovements.txt") Then
            Console.WriteLine("記録されたマウスの動きが見つかりません")
            Return
        End If

        Dim mouseMovements As String() = File.ReadAllLines("mouseMovements.txt")

        Console.WriteLine("マウスの動きを再生中...")

        Dim prevTime As Long = 0
        For Each movement As String In mouseMovements
            Dim parts As String() = movement.Split(","c)
            Dim x As Integer = Integer.Parse(parts(0))
            Dim y As Integer = Integer.Parse(parts(1))
            Dim timestamp As Long = Long.Parse(parts(2))

            If prevTime > 0 Then
                Dim delay As Integer = CInt((timestamp - prevTime) / TimeSpan.TicksPerMillisecond)
                Thread.Sleep(delay)
            End If

            ' マウスカーソルを移動
            SetCursorPos(x, y)

            prevTime = timestamp
        Next

        Console.WriteLine("再生が終了しました")
    End Sub
End Module

説明:

  1. 記録: GetCursorPos を使って現在のマウスの座標を取得し、タイムスタンプとともにリストに保存します。Escキーを押すと記録が終了します。

  2. 再生: 記録されたファイルを読み込み、座標ごとにマウスカーソルをその位置に移動します。前のタイムスタンプと比較して、再生時に同じ遅延を再現します。

2. コンパイル方法

以下のコマンドを使用して、vbc.exe を用いてそれぞれのファイルをコンパイルします。

Module1.vb (記録用プログラムのコンパイル)

vbc.exe /out:RecordMouse.exe Module1.vb
 

Module2.vb (再生用プログラムのコンパイル)

vbc.exe /out:ReplayMouse.exe Module2.vb