특정 이미지를 인식하고 특정 버튼을 누르는 Visual Basic의 오토마우스 코드 예시를 제시해 드리겠습니다. 다음은 비쥬얼베이직의 예시 코드입니다:
vb
Copy code
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Public Class MainForm
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
Private Declare Auto Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As IntPtr
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 3초 대기
Thread.Sleep(3000)
' 반복문으로 이미지 인식 및 버튼 누르기
For i As Integer = 1 To 5
' 이미지 인식 및 좌표 얻기
Dim imageLocation As Point = FindImage("image.png")
' 이미지를 찾은 경우에만 버튼 누르기
If Not imageLocation.IsEmpty Then
' 이미지를 찾은 윈도우로 포커스 이동
Dim targetWindow As IntPtr = FindWindow(Nothing, "Target Window Title")
SetForegroundWindow(targetWindow)
' 좌표로 마우스 이동 및 클릭
Cursor.Position = imageLocation
MouseClick(MouseButtons.Left)
End If
' 1초 대기
Thread.Sleep(1000)
Next i
End Sub
Private Function FindImage(imagePath As String) As Point
Dim bmpScreen As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim gfxScreen As Graphics = Graphics.FromImage(bmpScreen)
gfxScreen.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreen.Size)
Dim bmpImage As Bitmap = New Bitmap(imagePath)
Dim gfxImage As Graphics = Graphics.FromImage(bmpImage)
Dim imageLocation As Point = Nothing
Dim searchRect As Rectangle = New Rectangle(0, 0, bmpScreen.Width - bmpImage.Width, bmpScreen.Height - bmpImage.Height)
For y As Integer = searchRect.Top To searchRect.Bottom
For x As Integer = searchRect.Left To searchRect.Right
Dim found As Boolean = True
For j As Integer = 0 To bmpImage.Height - 1
For i As Integer = 0 To bmpImage.Width - 1
Dim pixelScreen As Color = bmpScreen.GetPixel(x + i, y + j)
Dim pixelImage As Color = bmpImage.GetPixel(i, j)
If pixelScreen <> pixelImage Then
found = False
Exit For
End If
Next i
If Not found Then
Exit For
End If
Next j
If found Then
imageLocation = New Point(x, y)
Exit For
End If