Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 109,297 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,205 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Scan Screen for Color?

 
Reply to this topicStart new topic

Scan Screen for Color?

animedude123
post 8 Aug, 2008 - 12:53 PM
Post #1


New D.I.C Head

*
Joined: 3 Aug, 2007
Posts: 18


My Contributions


Hey,

I'm looking into making a small macro sorta program and I'm wondering if there was a way in VB6 (Or VB.NET, either will do but I posted it here), to scan the screen for a certain pixel color (preferably a hex color value, but any will do) and then move the mouse to that spot.

I'm a complete beginner with VB and was hoping for help in anyway.

Thanks in advance for any help!
User is offlineProfile CardPM

Go to the top of the page


Zhalix
post 8 Aug, 2008 - 09:16 PM
Post #2


D.I.C Head

**
Joined: 7 May, 2008
Posts: 217



Thanked 9 times
My Contributions


It's really quite simple to do this in VB 6. You'll need the help of two API calls:

vb
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long


Get the screen's hDC like this:
vb
lngScreen = GetWindowDC(0)


And then using this line of code you can retrieve a pixel's color:
vb
lngColor = GetPixel(lngScreen, X, Y)


Simply cycle through all the pixels in the screen with a For-Loop. If being in base-16 is important then you can use the Hex() function to convert lngColor into Hex before you do your comparison.
User is offlineProfile CardPM

Go to the top of the page

animedude123
post 9 Aug, 2008 - 10:52 AM
Post #3


New D.I.C Head

*
Joined: 3 Aug, 2007
Posts: 18


My Contributions


QUOTE(Zhalix @ 8 Aug, 2008 - 09:16 PM) *

It's really quite simple to do this in VB 6. You'll need the help of two API calls:

vb
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long


Get the screen's hDC like this:
vb
lngScreen = GetWindowDC(0)


And then using this line of code you can retrieve a pixel's color:
vb
lngColor = GetPixel(lngScreen, X, Y)


Simply cycle through all the pixels in the screen with a For-Loop. If being in base-16 is important then you can use the Hex() function to convert lngColor into Hex before you do your comparison.


Will

lngColor = GetPixel(lngScreen, X, Y)

give me the coord to the pixel of the certain color that I want, or will it give me the pixel of a coord?

If it gives me the coord of the wanted pixel color, thanks, this is exactly what I was looking for biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Zhalix
post 9 Aug, 2008 - 05:53 PM
Post #4


D.I.C Head

**
Joined: 7 May, 2008
Posts: 217



Thanked 9 times
My Contributions


It gives you the color of the pixel. Say you wanted to know the color of the pixel at X: 242, Y: 440, then you'd say:

CODE
lngColor = GetPixel(lngScreen, 242, 440)


And then to check if that color is the one you want to move your mouse to, you'd do this:

CODE
If lngColor = lngRightColor Then
    'Code
End If


I forgot to mention that you'd also need to use the following API to move your mouse to the location of that pixel:
CODE
Private Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long
User is offlineProfile CardPM

Go to the top of the page

animedude123
post 9 Aug, 2008 - 10:48 PM
Post #5


New D.I.C Head

*
Joined: 3 Aug, 2007
Posts: 18


My Contributions


QUOTE(Zhalix @ 9 Aug, 2008 - 05:53 PM) *

It gives you the color of the pixel. Say you wanted to know the color of the pixel at X: 242, Y: 440, then you'd say:

CODE
lngColor = GetPixel(lngScreen, 242, 440)


And then to check if that color is the one you want to move your mouse to, you'd do this:

CODE
If lngColor = lngRightColor Then
    'Code
End If


I forgot to mention that you'd also need to use the following API to move your mouse to the location of that pixel:
CODE
Private Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long



Oh yea, does the pixel color get returned as a hex or something else? I'm guessing it gets it as long because API GetPixel line; "As Long, ByVal Y As Long) As Long"
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 09:44AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month