Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto Rolling

Posted on 2004-08-12
5
Medium Priority
?
234 Views
Last Modified: 2012-05-05
Hey Experts,

I was wondering if you could make a VB program which would check an outside application on specific pixles to check the colour.

Like:

If ....... = blue Then
    End
Else
   Roll

Where the "......" would specific a particular region of pixels to see if they were blue (or some of them). If not then there is a button on the outside application that needs to be clicked (Roll). And that will be looped.

Seeing it in my head it shouldn't be all that hard, but i have no clue on how to get a program to check something from another program that isn't part of it.

p.s. If you really don't get what i mean i can try to explain it better, or i'll make an attempt too....

0
Comment
Question by:Paranormal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11789029
Hi, try this:

Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Sub Form_Load()
    Dim lDC As Long, lhWnd As Long, lPixel As Long
    lhWnd = 'put hwnd of an outside control here (you can use FindWindow API to find it)
    lDC = GetDC(lhWnd)
    lPixel = GetPixel(lDC, 1, 1)
    MsgBox lPixel
End Sub

Darko
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 700 total points
ID: 11789904
The following code shows how to read the color of any pixel on the screen using the desktop device context.  The app simply shows the color of the pixel in RGB values wherever the mouse pointer currently is.

There is an additional sub called clickPoint() that will click the coordiantes of the point passed in.

Using the code in this example, you should be able to build the app you described.

Regards,

Idle_Mind


The project below needs a timer and two labels.

Option Explicit

Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As pointAPI) As Long

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _
    ByVal y As Long) As Long

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _
    ByVal y As Long) As Long

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)

Private Type pointAPI
    x As Long
    y As Long
End Type

Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Private desktopDC As Long

Private Sub Form_Load()
    Timer1.Interval = 100
    desktopDC = GetWindowDC(GetDesktopWindow())
End Sub

Private Sub Timer1_Timer()
    Dim z As pointAPI
    Dim pixelColor As Long
    Dim red As Byte
    Dim green As Byte
    Dim blue As Byte
   
    GetCursorPos z
    Label1.Caption = "Coordinates: " & z.x & ", " & z.y
    pixelColor = GetPixel(desktopDC, z.x, z.y)
   
    red = pixelColor Mod &H100
    pixelColor = pixelColor \ &H100
    green = pixelColor Mod &H100
    pixelColor = pixelColor \ &H100
    blue = pixelColor Mod &H100
    Label2.Caption = "(R, G, B) Values = " & red & ", " & green & ", " & blue
End Sub

Private Sub clickPoint(ByVal x As Single, ByVal y As Single)
    Call SetCursorPos(x, y)
    mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, x, y, 0, 0
End Sub
0
 

Author Comment

by:Paranormal
ID: 11791153
Thanks a lot, i'm not done yet tho, i was wondering if i could make a command button, to make it run ofcourse and where i can put in the statements.

Private Sub Command1_Click()
If coordinates: 130, 340 = colour: 235, 234, 24 Then
   clickPoint
Else
    End
End Sub

I know i didn't write it all VB-like but thats because i wouldn't know how, while still making it understandable. And on top of this, can you make a region, like between (coordinates) 130, 340 and 140, 300 and then have em check every coordinate point inbetween to look if it has that specific pixel color?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11792370
Below are the two examples you asked for...the first checks the specific pixel and the second checks the entire region.  You could place the code in the second example in the timer event if you want it to repeat.

Private Sub Command1_Click()
    Dim pixelcolor As Long
    Dim red As Byte
    Dim green As Byte
    Dim blue As Byte
   
    pixelcolor = GetPixel(desktopDC, 130, 340)
       
    red = pixelcolor Mod &H100
    pixelcolor = pixelcolor \ &H100
    green = pixelcolor Mod &H100
    pixelcolor = pixelcolor \ &H100
    blue = pixelcolor Mod &H100
   
    If red = 235 And green = 234 And blue = 24 Then
        clickPoint 500, 250 ' <----- change these coordinates to where you want to click
    Else
        ' not sure what you mean by end here
    End If
End Sub

Private Sub Command2_Click()
    Dim pixelcolor As Long
    Dim x As Single
    Dim y As Single
    Dim red As Byte
    Dim green As Byte
    Dim blue As Byte
   
    ' between (coordinates) 130, 300 and 140, 340
    For x = 130 To 140
        For y = 300 To 340
            pixelcolor = GetPixel(desktopDC, x, y)
       
            red = pixelcolor Mod &H100
            pixelcolor = pixelcolor \ &H100
            green = pixelcolor Mod &H100
            pixelcolor = pixelcolor \ &H100
            blue = pixelcolor Mod &H100
   
            If red = 235 And green = 234 And blue = 24 Then
                clickPoint 500, 250 ' <----- change these coordinates to where you want to click
                Exit Sub
            End If
        Next y
    Next x
End Sub
0
 

Author Comment

by:Paranormal
ID: 11792568
Thank you for all that you showed me and gave me, it's still hard trying to mix it with the actual application i wanted to test it on but at least i got the basics and i believe i can figure the rest out myself,

Thank you very much.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question