Can VB.net Click colors?

Discussion in 'Programming General' started by dgameman1, Sep 5, 2011.

Can VB.net Click colors?
  1. Unread #1 - Sep 5, 2011 at 6:26 PM
  2. dgameman1
    Joined:
    May 25, 2006
    Posts:
    122
    Referrals:
    0
    Sythe Gold:
    0

    dgameman1 Active Member
    Banned

    Can VB.net Click colors?

    Is it possible for VB.net to click colors on a screen? and if so, how?
     
  3. Unread #2 - Sep 5, 2011 at 9:59 PM
  4. Terrankiller
    Joined:
    May 7, 2005
    Posts:
    1,286
    Referrals:
    1
    Sythe Gold:
    1

    Terrankiller Ex-Administrator
    Retired Administrator Visual Basic Programmers

    Can VB.net Click colors?

    Yes it is possible. There are many ways to do this for instance we will need the GetPixel API to get the pixel color value from a specific point:

    Code:
    Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As IntPtr, ByVal x As Int32, ByVal y As Int32) As Int32
    
    We will also need a few other APIs to make this work:

    Code:
    Public Declare Function GetWindowDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
    Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal ByValhdc As IntPtr) As Integer
    
    Now how do we make this work? Let me show you an example of how this will work:

    Code:
    Dim PixelColor as Int32
    PixelColor = GetPixel(windowDC, X, Y)
    
    GetPixel will return the specified pixel color as a 32 bit integer from the x and y coordinates on the specified window device context that you have provided. You might be wondering how to get the window dc, well we will use the GetWindowDC API for that:

    Code:
    Dim windowDC as IntPtr
    windowDC = GetWindowDC(PictureBox1.Handle)
    
    We will be using a PictureBox for this example. We got the handle of the PictureBox by using the controls handle property. Then we used GetWindowDC to return the device context of the picturebox to windowDC.

    Now how do we use this code to find a color? Well you can program a search function like this:

    Code:
        Public Sub FindColor(ByVal hWND As IntPtr, ByVal Color As Int32, ByRef X As Int32, ByRef Y As Int32)
            Dim hDC As Int32
            hDC = GetWindowDC(hWND)
            For iX = 0 To 100
                For iY = 0 To 100
                    If GetPixel(hDC, iX, iY) = Color Then
                        ReleaseDC(hWND, hDC)
                        X = iX
                        Y = iY
                        Exit Sub
                    End If
                Next
            Next
            ReleaseDC(hWND, hDC)
            X = -1
            Y = -1
        End Sub
    
    If the color you have specified is found then it will return the coordinates of the pixel to X and Y. If the color is not found it will return -1 to X and Y.

    Now how do we use this function you say? Well lets make a new form with with a picturebox and a button. Set the picture or background color of the picture box. Now lets program the button code:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim px As Int32
            Dim py As Int32
    
            FindColor(PictureBox1.Handle, 255, px, py)
    
            If px = -1 And py = -1 Then
                MsgBox("Not found")
            Else
                MsgBox(px.ToString & ", " & py.ToString)
            End If
    
        End Sub
    
    If the color is not found we will use a message box to say we didn't find it. If the color is found we will use the message box to display the coordinates.

    Now for mouse clicking we are going to need another API call:

    Code:
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dx As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32)
    
    We will also need 2 constant variables:

    Code:
    Public Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
    Public Const MOUSEEVENTF_LEFTUP As Int32 = &H4
    
    With this we can write a mouse clicking function:

    Code:
        Public Sub mClick(ByVal X As Int32, ByVal Y As Int32)
            Dim cPoint As Point
            cPoint.X = X
            cPoint.Y = Y
            Cursor.Position = cPoint
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        End Sub
    
    This function will move the mouse cursor to the specified coordinates you have provided and invoke a left mouse click.

    An example of this would be:

    Code:
    mClick(X,Y)
    
    The problem here is you will need to modify the returned coordinates of the find color function because we got the coordinates based from the window handle. So we will need yet another API call:

    Code:
     Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As IntPtr, ByRef rect As Rectangle) As Boolean
    
    Here is an example on how to use this:

    Code:
    Dim rWND As System.Drawing.Rectangle
    GetWindowRect(hWND, rWND)
    
    Now how do we use this to get modify our returned mouse coordinates? Simple:

    Code:
    mClick(rWND.X +X, rWND.Y + Y)
    
    We are done now. That is the shitty guide I wrote while I was bored for the past hour. Here is the finished code:

    Code:
    Public Class Form1
    
        Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As IntPtr, ByVal x As Int32, ByVal y As Int32) As Int32
        Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
        Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal ByValhdc As IntPtr) As Integer
        Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dx As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32)
        Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As IntPtr, ByRef rect As Rectangle) As Boolean
    
        Public Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2
        Public Const MOUSEEVENTF_LEFTUP As Int32 = &H4
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim rWND As System.Drawing.Rectangle
            Dim px As Int32
            Dim py As Int32
    
            FindColor(PictureBox1.Handle, 255, px, py)
    
            If px = -1 And py = -1 Then
                MsgBox("Not found")
            Else
                MsgBox(px.ToString & ", " & py.ToString)
                GetWindowRect(PictureBox1.Handle, rWND)
                mClick(rWND.X + px, rWND.Y + py)
            End If
    
        End Sub
    
        Public Sub FindColor(ByVal hWND As IntPtr, ByVal Color As Int32, ByRef X As Int32, ByRef Y As Int32)
    
            Dim hDC As Int32
    
            hDC = GetWindowDC(hWND)
    
            For iX = 0 To 100
                For iY = 0 To 100
                    If GetPixel(hDC, iX, iY) = Color Then
                        ReleaseDC(hWND, hDC)
                        X = iX
                        Y = iY
                        Exit Sub
                    End If
                Next
            Next
    
            ReleaseDC(hWND, hDC)
            X = -1
            Y = -1
    
        End Sub
    
        Public Sub mClick(ByVal X As Int32, ByVal Y As Int32)
    
            Dim cPoint As Point
    
            cPoint.X = X
            cPoint.Y = Y
            Cursor.Position = cPoint
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    
        End Sub
    
    End Class
    
    There are other ways to do this but this is just one way to do it. We could also create a bitmap image and get the pixels from it which would be much quicker.
     
  5. Unread #3 - Sep 5, 2011 at 11:57 PM
  6. dgameman1
    Joined:
    May 25, 2006
    Posts:
    122
    Referrals:
    0
    Sythe Gold:
    0

    dgameman1 Active Member
    Banned

    Can VB.net Click colors?

    Can you please get on MSN? I would love to talk to you 1on 1?

    I loved the tutorial, thank you sooo much
    but the error i get is


    Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
     
  7. Unread #4 - Sep 6, 2011 at 8:08 AM
  8. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Can VB.net Click colors?


    You must have mis-copied something or edited TerranKiller's code wrong, as there are no errors in the code he posted.
    Please post what part of the code VS highlights when it throws the error.
     
  9. Unread #5 - Sep 7, 2011 at 11:21 PM
  10. dgameman1
    Joined:
    May 25, 2006
    Posts:
    122
    Referrals:
    0
    Sythe Gold:
    0

    dgameman1 Active Member
    Banned

    Can VB.net Click colors?

    Ok I fixed it, It works perfectly now, the problem is, how can I make it click more than one color after eachother?
     
  11. Unread #6 - Sep 8, 2011 at 5:07 AM
  12. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Can VB.net Click colors?

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim rWND As System.Drawing.Rectangle
            Dim px As Int32
            Dim py As Int32
    
            [color=red]FindColor(PictureBox1.Handle, 255, px, py)
    
            If px = -1 And py = -1 Then
                MsgBox("Not found")
            Else
                MsgBox(px.ToString & ", " & py.ToString)
                GetWindowRect(PictureBox1.Handle, rWND)
                mClick(rWND.X + px, rWND.Y + py)
            End If[/color]
    
        End Sub
    You can just copy and paste the part in red underneath it self and change the colour to find.

    Although this is definately a shit way to do it, you will need to use a loop or for statement, tell me exactly what you're doing and i'll try fix you up with a correct method.
     
  13. Unread #7 - Sep 8, 2011 at 8:19 PM
  14. dgameman1
    Joined:
    May 25, 2006
    Posts:
    122
    Referrals:
    0
    Sythe Gold:
    0

    dgameman1 Active Member
    Banned

    Can VB.net Click colors?

    What I want is so that when I press Button5, my mouse will look for a certain color in a certain area and if it is there, then it would click, after it clicks it, it does the same thing but with a different color in a different area and etc
     
  15. Unread #8 - Sep 8, 2011 at 9:38 PM
  16. Terrankiller
    Joined:
    May 7, 2005
    Posts:
    1,286
    Referrals:
    1
    Sythe Gold:
    1

    Terrankiller Ex-Administrator
    Retired Administrator Visual Basic Programmers

    Can VB.net Click colors?

    I am not going to write your whole program and you should not expect anyone else to do it for you. It is your job as the programmer to code the program.

    Learn VB.net


    It is better that you learn about programming in vb.net before you start a project like this. Now you will need to either educate yourself or pay for a class on the subject.
     
  17. Unread #9 - Sep 9, 2011 at 8:27 AM
  18. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Can VB.net Click colors?

  19. Unread #10 - Sep 9, 2011 at 6:48 PM
  20. dgameman1
    Joined:
    May 25, 2006
    Posts:
    122
    Referrals:
    0
    Sythe Gold:
    0

    dgameman1 Active Member
    Banned

    Can VB.net Click colors?

    My VB wont open this c++ file
     
  21. Unread #11 - Sep 9, 2011 at 11:41 PM
  22. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Can VB.net Click colors?

    i made that in vs 2010 and it's a vb project :/ (uses 4.0 framework)
     
< List of USEFUL Sources | Thread Options How can I find the 32 bit number of a color? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site