I want to make a macro that will move the mouse, click, use keys. Click somewhere that has a group Pixel colors. Like all the shades of red in a rose on a grassy landscape. Purpose, be good for games, and maybe photos*h*op. I could get more creative but those are the basic things.
Which language should I use? What libraries would you recommend?
Exactly what I would want this macro to do.
1. Switch to a window or program that's opened based off its name or whatever to identify it.
2. Click somewhere once based off a group of colors.
3. Sleep
4. Hit a key on the keyboard
5. Repeat
6. And if it DOESN'T find the group of colors to click, it should hit a key on the keyboard, I'm guessing I could figure that out myself with some googling. Doing ifs, elses, and all dat.
This is one of the reasons i like programming because you can do crazy things, even if you can't make this perfect, just point me somewhere so i can learn, or use something to help me create this macro.
Found this code snippet on google, it supposively does everything I want. How would I get this to work in VB express 2010? With a form application, and a button I couldn't get it to work. Is this code incorrect for my needs, show me alternatives?
Where i found the code:
http://thebotnet.com/programming/1810-vb-net-creating-rs-bot/
Functions:
FindControl
ClickControl
SendKey
SendChar
SendKeyString
SendString
GetScreenshot (Won't work when window is minimized)
GetPixelColor (Won't work when window is minimized)
GetControlSize
PixelSearch
ImageSearch (Added this since I thought it might be useful) (Won't work when window is minimized)
The code that supposed to use those functions.
Imports WindowsControls
Dim Handle As IntPtr = Controls.FindWindow(WebBrowser1.Handle, "ControlNameHere")
Dim MyControl As New Controls(Handle)
MyControl.ClickControl(123, 456)
System.Threading.Thread.Sleep(1000)
Dim MyColor As Color = MyControl.GetPixelColor(12, 34)
Dim ClickPoint As Point = MyControl.PixelSearch(MyColor)(0)
MyControl.ClickControl(ClickPoint)
Dim ClickPoint2 As Point = MyControl.ImageSearch(My.Resources.ExamplePic)(0)
MyControl.ClickControl(ClickPoint2)
The class were I'm guessing some of the functions were made?
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Namespace WindowsControls
Class Control
Public Sub New(ByVal Handle As IntPtr)
hWnd = Handle
End Sub
Private hWnd As IntPtr = IntPtr.Zero
Public Property ControlHandle() As IntPtr
Get
Return hWnd
End Get
Set(ByVal value As IntPtr)
hWnd = value
End Set
End Property
#Region "Interop"
Const WM_KEYDOWN As UInteger = &H100
Const WM_KEYUP As UInteger = &H101
Const WM_PAINT As Int32 = &HF
Const WM_LBUTTONDOWN As Int32 = &H201
Const WM_LBUTTONUP As Int32 = &H202
<DllImport("User32.dll", SetLastError:=True)> _
Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hDC As IntPtr, ByVal nFlags As UInteger) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal className As String, ByVal windowTitle As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
#End Region
Public Shared Function FindControl(ByVal parentHandle As IntPtr, ByVal controlName As String) As IntPtr
Return FindWindowEx(parentHandle, IntPtr.Zero, controlName, IntPtr.Zero)
End Function
Public Sub ClickControl(ByVal x As Integer, ByVal y As Integer)
Dim coords As Integer = (y << 16) + x
Dim lParam As New IntPtr(coords)
PostMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam)
PostMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, lParam)
End Sub
Public Sub ClickControl(ByVal p As Point)
ClickControl(p.X, p.Y)
End Sub
Public Sub SendKey(ByVal k As Keys)
PostMessage(hWnd, WM_KEYDOWN, New IntPtr(CInt(k)), IntPtr.Zero)
PostMessage(hWnd, WM_KEYUP, New IntPtr(CInt(k)), IntPtr.Zero)
End Sub
Public Sub SendChar(ByVal c As Char)
Const WM_CHAR As UInteger = &H102
PostMessage(hWnd, WM_CHAR, New IntPtr(Convert.ToInt32(c)), IntPtr.Zero)
End Sub
Public Sub SendKeyString(ByVal s As String)
s = s.ToLower()
For Each c As Char In s
SendKey(DirectCast((Convert.ToInt32(c) - 32), Keys))
Next
End Sub
Private Sub SendString(ByVal s As String)
For Each c As Char In s
SendChar(c)
Next
End Sub
Public Function GetScreenshot() As Bitmap
Dim controlSize As Size = GetControlSize()
Dim bmp As New System.Drawing.Bitmap(controlSize.Width, controlSize.Height)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
Dim dc As IntPtr = g.GetHdc()
SendMessage(hWnd, WM_PAINT, IntPtr.Zero, IntPtr.Zero)
PrintWindow(hWnd, dc, 0)
g.ReleaseHdc()
g.Dispose()
Return bmp
End Function
Public Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As Color
Return GetScreenshot().GetPixel(x, y)
End Function
Public Function GetPixelColor(ByVal p As Point) As Color
Return GetPixelColor(p.X, p.Y)
End Function
Public Function GetControlSize() As Size
Dim rect As RECT
GetWindowRect(hWnd, rect)
Return New Size(rect.Right - rect.Left, rect.Bottom - rect.Top)
End Function
Public Function PixelSearch(ByVal c As Color) As Point()
Dim list As New System.Collections.ArrayList()
Dim controlSize As Size = GetControlSize()
Dim controlImage As Bitmap = GetScreenshot()
For y As Integer = 0 To controlSize.Height - 1
For x As Integer = 0 To controlSize.Width - 1
If controlImage.GetPixel(x, y) = c Then
list.Add(New Point(x, y))
End If
Next
Next
Return DirectCast(list.ToArray(GetType(Point)), Point())
End Function
Public Function ImageSearch(ByVal b As Bitmap) As Point()
Dim controlSize As Size = GetControlSize()
Dim controlImage As Bitmap = GetScreenshot()
Dim list As New ArrayList()
For y As Integer = 0 To controlSize.Height - 1
For x As Integer = 0 To controlSize.Width - 1
If controlImage.GetPixel(x, y) = b.GetPixel(0, 0) Then
Dim validPoint As Boolean = True
For by As Integer = 0 To b.Height - 1
For bx As Integer = 0 To b.Width - 1
If x + bx >= controlImage.Width OrElse y + by >= controlImage.Height Then
validPoint = False
Exit For
ElseIf controlImage.GetPixel(x + bx, y + by) <> b.GetPixel(bx, by) Then
validPoint = False
Exit For
End If
Next
If Not validPoint Then
Exit For
End If
Next
If validPoint Then
list.Add(New Point(x, y))
End If
End If
Next
Next
Return DirectCast(list.ToArray(GetType(Point)), Point())
End Function
Public Function ImageSearch(ByVal i As Image) As Point()
Return ImageSearch(DirectCast(i, Bitmap))
End Function
End Class
End Namespace
Too much p/invoke does that mean param invoking? I agree it doesn't look like VB.net code, lots of XML thrown in there.
I only know, HTML, CSS.
I've looked at other languages, and read up on them. I know that code has containers, and ways to start/end it. I know that imports go at the top. I know functions can have inputs, and outputs. I know that things are called, or linked to a lot in order to do whatever. Like some functions you have to link to them, also i think if you use namespaces or if you use imports you don't have to link to them sometimes or as much. There's tons of ways to structure code, if statements. I know that a class is where all the hard stuff goes, and you link to them sometimes just to use a function in it. Libraries you link to them to use functions, or whatever. Etc etc. I read up a lot on coding but i forget most of it. cout << printf writeDocument, bah i forget.
Also I don't know a lot of the vocabularly associated with coding. Like if you say your putting a sub base in a class object overflowing the namespace. I'm gonna O_O, just be aware I'd have to google everything your saying =_=
Thank you for looking at this... And to those that looked at my last question on this website