Got bored so I’ve decided that I’m going to make a little macro program, and ill post the updates here.
Currently, I’ve got the basic structure and events working and I’ve tested it on a reactions tester to see how efficient it was.
Currently, it’s started using a HotKey, and detects the message and starts a background worker.
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = WM_HOTKEY Then
If BackgroundWorker1.IsBusy = False Then
BackgroundWorker1.RunWorkerAsync()
stops = False
Else
stops = True
End If
ElseIf m.Msg = WM_QUERYENDSESSION Then
stops = True
Application.Exit()
End If
End Sub
The stops is a global Boolean and is in there as a separate way to end the macro’s loop, in case something has gone wrong. The first press of the HotKey starts the macro, the second ends it.
ElseIf m.Msg = WM_QUERYENDSESSION Then
Is just in there if the message detected is the Windows Shutdown message, the application closes and ends the loop
Now, we have to look at how exactly we are going to create our mouse functions. Firstly, let’s look at the basic mouse click:
I’m carrying out this function using the mouse_event library:
Private 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)
This is used with the below mouse event constants:
Const MOUSEEVENTF_MOVE As Int32 = &H1
Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
Const MOUSEEVENTF_LEFTUP As Int32 = &H4
Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8
Const MOUSEEVENTF_RIGHTUP As Int32 = &H10
Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20
Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40
Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000
Const MOUSEEVENTF_WHEEL As Int32 = &H800
For the sake of ease, we will only be using the dwFlags option of the mouse_events library, in order to peform a mouse click.
For a left mouse click:
mouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
This will click the mouse at the cursors current location.
Next, we will look at moving the mouse to specified coordinates.
This isn’t hard, you can either move the mouse using the mouse_event library or you can set the cursor location like this:
Windows.Forms.Cursor.Position = New Point(700, 600)
This will move the mouse location to the specified point on the screen.
Next what we will look at is Pixel colour detection. This is done using the GetPixel and CreateDC API:
<Runtime.InteropServices.DllImport(“gdi32.dll”)> Private Shared Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As Integer
End Function
<Runtime.InteropServices.DllImport(“gdi32.dll”)> Private Shared Function CreateDC(ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr
End Function
<Runtime.InteropServices.DllImport(“gdi32.dll”)> Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
End Function
We create a function in order to easily provide us with information on the pixel colour:
Private Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As Color
”This gets the pixel colour from the specified x/y coordinates
Dim Scrn_hdc As IntPtr = CreateDC(“Display”, Nothing, Nothing, IntPtr.Zero)
”Display being the name of the driver (Aka the screen display)
Dim Colour As Integer = GetPixel(Scrn_hdc, x, y)
”Gets pixel information from this hdc
DeleteDC(Scrn_hdc)
Return Color.FromArgb(Colour And &HFF, (Colour And &HFF00) >> 8, (Colour And &HFF0000) >> 16)
”Returns the colour value
End Function
We now have everything we need to create a basic pixel checker.
Now, back to the Background Worker. This is where we will do the pixel checking, on a loop
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim count As Integer = 0
Do
If GetPixelColor(530, 540) <> Color.FromArgb(255, 255, 0, 0) Then
”Waits for the colour at that point to change from Red
Windows.Forms.Cursor.Position = New Point(530, 540)
”Moves the mouse cursor
mouse_event(MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
‘ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
”Clicks the mouse
count += 1
”Adds one to count
Windows.Forms.Cursor.Position = New Point(700, 600)
”Moves the mouse cursor out of the way
System.Threading.Thread.Sleep(50)
”Sleeps the thread
If count = 5 Then
”If its done 5 clicks, it sets stops to true
stops = True
End If
End If
System.Threading.Thread.Sleep(1)
”Pause the loop
Loop Until stops = True ‘
‘Loop until stops is true, either set when count = 5 or externally
End Sub
This is just a basic example. It checks the pixel colour at 530,540 and if it is not Red it moves the mouse to that location and clicks. It then moves the mouse off of that location and adds one to the loop.
If the loop has been carried out 5 times, it then sets stops to true and the macro is stopped.