Macro functions and procedures

Discussion in 'Programming General' started by AB, Mar 28, 2008.

Macro functions and procedures
  1. Unread #1 - Mar 28, 2008 at 11:03 PM
  2. AB
    Joined:
    Dec 19, 2005
    Posts:
    141
    Referrals:
    3
    Sythe Gold:
    10

    AB Active Member

    Macro functions and procedures

    Here are some of my autoing functions and procedures. There is an explanation on how to use them included with the source.
    These functions and procedures are for your learning/use | Please give credit.​

    Mouse​

    Public Function mousePos() As System.Drawing.Point
    Usage: msgBox('This is the mouse position: ' & mousePos.tostring)
    Explanation: Returns the mouse position as a system.drawing.point.
    Parameters: None
    Code:
    	Public Function mousePos() As System.Drawing.Point
    		mousePos = Windows.Forms.Cursor.Position()
    	End Function
    Public Sub moveMouse(ByVal x As Integer, ByVal y As Integer)
    Usage: moveMouse(100,100)
    Explanation: Sets the mouse position to x,y
    Parameters: x - X postition on the screen; y - Y position on the screen
    Code:
    	Public Sub moveMouse(ByVal x As Integer, ByVal y As Integer)
    		Windows.Forms.Cursor.Position = New Point(x, y)
    	End Sub
    
    Public Sub moveMouseSmooth(ByVal x As Integer, ByVal Y As Integer, ByVal steps As Integer)
    Usage: moveMouseSmooth(100,100,300)
    Explanation: Moves the mouse to the position x,y,like a human would. < steps = smoother
    Parameters: x - X postition on the screen; y - Y position on the screen; steps - number of times the cursor will "jump" in a line
    Code:
    	Public Sub moveMouseSmooth(ByVal x As Integer, ByVal Y As Integer, ByVal steps As Integer)
    		Dim diffX As Integer
    		Dim diffY As Integer
    		Dim I As Integer
    
    		If mousePos.X > x Then
    			diffX = mousePos.X - x
    		Else : diffX = x - mousePos.X
    		End If
    
    		If mousePos.Y > Y Then
    			diffY = mousePos.Y - Y
    		Else : diffY = Y - mousePos.Y
    		End If
    
    		diffX = diffX / steps
    		diffY = diffY / steps
    
    		For I = 1 To steps
    			If mousePos.X > x Then
    				moveMouse(mousePos.X - diffX, mousePos.Y)
    			Else : moveMouse(mousePos.X + diffX, mousePos.Y)
    			End If
    
    			If mousePos.Y > Y Then
    				moveMouse(mousePos.X, mousePos.Y - diffY)
    			Else : moveMouse(mousePos.X, mousePos.Y + diffY)
    			End If
    
    			wait(1)
    		Next
    	End Sub
    
    Public Sub clickMouse(ByVal x As Integer, ByVal y As Integer, ByVal left As Boolean)
    Usage: clickMouse(100,100,true)
    Explanation: Clicks the mouse at the postition x,y
    Parameters: x - X postition on the screen; y - Y position on the screen; left - true = left click | false = right click
    Code:
    	Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Int32, ByVal dx As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32)
    	Public Sub clickMouse(ByVal x As Integer, ByVal y As Integer, ByVal left As Boolean)
    		moveMouse(x, y)
    		If left Then
    			mouse_event(&H2, 0, 0, 0, 0)
    			mouse_event(&H4, 0, 0, 0, 0)
    		Else
    			mouse_event(&H8, 0, 0, 0, 0)
    			mouse_event(&H10, 0, 0, 0, 0)
    		End If
    	End Sub
    Public Sub clickMouseEx(ByVal x As Integer, ByVal y As Integer, ByVal randomX As Integer, ByVal randomY As Integer, ByVal waitTime As Integer, ByVal randomWaitTime As Integer, ByVal left As Boolean)
    Usage: clickMouseEx(100,100,3,3,20,30,true)
    Explanation: Clicks the mouse at the postition x,y like a human.
    Parameters: x - X postition on the screen; y - Y position on the screen; randomX - ammount of pixels to randomly add to "x"; randomY - ammount of pixels to randomly add to "y"; waitTime - how long to wait to between moving the mouse and clicking the mouse in ms; randomWaitTime - randomly adds to "waitTime" in ms; left - true = left click | false = right click
    Code:
    	Public Sub clickMouseEx(ByVal x As Integer, ByVal y As Integer, ByVal randomX As Integer, ByVal randomY As Integer, ByVal waitTime As Integer, ByVal randomWaitTime As Integer, ByVal left As Boolean)
    		moveMouseSmooth(x + Rnd(randomX), y + Rnd(y))
    		wait(waitTime + Rnd(randomWaitTime))
    		clickMouse(mousePos.X, mousePos.Y, left)
    	End Sub


    Bitmaps​

    Private Function getBitmap(ByVal area As Rectangle) As Bitmap
    Usage: dim bmp as new bitmap(100,100) = getBitmap(new rectangle(1,1,100,100))
    Explanation: Copies the selected screen section (area) and returns it.
    Parameters: area - the selected screen section to be copied.
    Code:
    	Private Function getBitmap(ByVal area As Rectangle) As Bitmap
    		Dim areasize As Size = area.Size
    		Dim tempBitmap As New Bitmap(areasize.Width, areasize.Height)
    		Graphics.FromImage(tempBitmap).CopyFromScreen(area.Location, New Point(), areasize)
    		Return tempBitmap
    	End Function
    Private Function findBitmapIn(ByRef x As Integer, ByRef Y As Integer, ByVal targetBitmap As Bitmap, ByVal zone As Rectangle) As Boolean
    Usage: if findBitmapIn(x,y,bmp,new rectangle(1,1,100,100)) then
    Explanation: Searches the screen for "targetBitmap", if found the returns true.
    Parameters: x - Your variable that the position of the found bitmap will return; y - Your variable that the position of the found bitmap will return; targetBitmap - The bitmap that will be searched for; zone - The area that will be scanned.
    Code:
    	Private Function findBitmapIn(ByRef x As Integer, ByRef Y As Integer, ByVal targetBitmap As Bitmap, ByVal zone As Rectangle) As Boolean
    		Dim areaSize As Size = zone.Size
    		Dim tempBitmap As New Bitmap(areaSize.Width, areaSize.Height)
    		Graphics.FromImage(tempBitmap).CopyFromScreen(zone.Location, New Point(), areaSize)
    
    		Dim targerColor As System.Drawing.Color = targetBitmap.GetPixel(1, 1)
    		For scanX = 1 To (tempBitmap.Width - targetBitmap.Height)
    			For scanY = 1 To (tempBitmap.Height - targetBitmap.Height)
    				If tempBitmap.GetPixel(scanX, scanY) = targerColor Then
    
    					Dim targetSize As Size = targetBitmap.Size
    					Graphics.FromImage(tempBitmap).CopyFromScreen(New Point(scanX, scanY), New Point(), targetSize)
    
    					For scanX2 As Integer = 1 To targetBitmap.Width - 1
    						For scanY2 As Integer = 1 To targetBitmap.Height - 1
    							If Not targetBitmap.GetPixel(x, Y) = tempBitmap.GetPixel(x, Y) Then
    								Return False
    								Exit Function
    							End If
    						Next : Next
    				End If
    			Next : Next
    		Return False
    	End Function


    Colors​

    Function getColor(ByVal x As Integer, ByVal y As Integer) As Integer
    Usage: dim myColor as integer = getColor(100,100)
    Explanation: Will return the color of the pixel at x,y.
    Parameters: x - the X position of the pixel; y - the Y position of the pixel.
    Code:
    	Function getColor(ByVal x As Integer, ByVal y As Integer) As Integer
    		Dim areasize As Size = New Rectangle(x, y, 1, 1).Size
    		Dim tempBitmap As New Bitmap(areasize.Width, areasize.Height)
    		Graphics.FromImage(tempBitmap).CopyFromScreen(New Rectangle(x, y, 1, 1).Location, New Point(), areasize)
    
    		Dim tempColor As System.Drawing.Color = tempBitmap.GetPixel(x, y)
    		Return Color.FromArgb(255, tempColor.R, tempColor.G, tempColor.B).GetHashCode()
    	End Function
    Function findcolor(ByRef x As Integer, ByRef y As Integer, ByVal TargetColor As Integer, ByVal area As Rectangle) As Boolean
    Usage: if findColor(x,y,myColor,new rectangle(1,1,100,100)
    Explanation: Will scan for "TargetColor" on the screen, if found the will return true.
    Parameters: x - Your variable that the position of the found color will return; y - Your variable that the position of the found color will return; TargetColor- The color that will be searched for; area - The area that will be scanned.
    Code:
    	Function findcolor(ByRef x As Integer, ByRef y As Integer, ByVal TargetColor As Integer, ByVal area As Rectangle) As Boolean
    		Dim areasize As Size = area.Size
    		Dim tempBitmap As New Bitmap(areasize.Width, areasize.Height)
    		Graphics.FromImage(tempBitmap).CopyFromScreen(area.Location, New Point(), areasize)
    
    		Dim tempColor As System.Drawing.Color
    		Dim convertedColor As Integer
    		For scanX = 1 To area.Width - 1
    			For scanY = 1 To area.Height - 1
    				tempColor = tempBitmap.GetPixel(x, y)
    				convertedColor = Color.FromArgb(255, tempColor.R, tempColor.G, tempColor.B).GetHashCode()
    				If (convertedColor = TargetColor) Then
    					Return True
    					Exit Function
    				End If
    			Next : Next
    		Return False
    	End Function

    Please Comment!​


    - AB
     
  3. Unread #2 - Apr 6, 2008 at 6:28 AM
  4. nicknack
    Joined:
    Apr 25, 2005
    Posts:
    29
    Referrals:
    0
    Sythe Gold:
    0

    nicknack Member

    Macro functions and procedures

    Nice post :) Very Useful for leachers
     
  5. Unread #3 - Apr 6, 2008 at 6:50 AM
  6. hydrophobia
    Referrals:
    0

    hydrophobia Guest

    Macro functions and procedures

    Thanks
     
  7. Unread #4 - Apr 6, 2008 at 7:51 PM
  8. kharg0
    Joined:
    Feb 25, 2007
    Posts:
    131
    Referrals:
    0
    Sythe Gold:
    0

    kharg0 Active Member

    Macro functions and procedures

    Good Job looked 4 a while for these tried cruels.net but they spazed on me so.
     
  9. Unread #5 - Jul 12, 2008 at 12:56 PM
  10. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Macro functions and procedures

    oo awesome :) thx man... im kinda new to vb so it realy helped :D
     
  11. Unread #6 - Oct 10, 2008 at 12:04 AM
  12. Kinglighter
    Joined:
    Aug 11, 2007
    Posts:
    102
    Referrals:
    0
    Sythe Gold:
    0

    Kinglighter Active Member

    Macro functions and procedures

    Thanks a lot!
     
  13. Unread #7 - Oct 16, 2008 at 11:44 PM
  14. 128729972
    Referrals:
    0

    128729972 Guest

    Macro functions and procedures

    results in codes and how to use with sample code
    Color picker = 0%

    movemouse = 100%

    Deascription for move mouse make a form with one button leave them unnamed

    then add this code under Public Class Form1 ill explain the code below:
    Code:
     Public Sub moveMouse(ByVal x As Integer, ByVal y As Integer)
            Windows.Forms.Cursor.Position = New Point(x, y)
    
        End Sub
    
    
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            moveMouse(100, 100)
        End Sub
    
    
    
    Ok the move mouse is well the move mouse function now notice that in he button click you MUST put the parameters like that what it does is replace the x, y with those parameters.
     
< Java scripting | [Python] Desktop background changer >

Users viewing this thread
1 guest


 
 
Adblock breaks this site