Can you explain why it does not work?


Option Explicit On
Public Class Form1
Dim ProcessHandle As Long, ProcessDC As Long
Private Sub Form_Activate()
ProcessHandle = FindWindow(vbNullString, "Game")
ProcessDC = GetDC(0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call ReleaseDC(0, ProcessDC)
End Sub

Private Sub Timer1_Timer()
On Error Resume Next

Dim X As Long, Y As Long, PixelColor As Long
Dim CursorPosition As Point

If (ProcessHandle <> GetForegroundWindow()) Then
Exit Sub
End If

Call GetCursorPos(CursorPosition)

For X = CursorPosition.X - 20 To CursorPosition.X + 20
For Y = CursorPosition.Y - 20 To CursorPosition.Y + 20
PixelColor = GetPixel(ProcessDC, X, Y)

'Replace RGB value for your specified color
If (PixelColor = RGB(231, 137, 140)) Then
Call mouse_event(MOUSEEVENTF_LEFTDOWN, CursorPosition.X, CursorPosition.Y, 0, 0)
Exit Sub
End If
Next Y
Next X
End Sub

Option Explicit On
Module PixelScan

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetCursorPos Lib "user32" (lpPoint As Point) As Long
Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long


Public Structure Point
Dim X As Long
Dim Y As Long
End Structure
End Module