How can i specify a X and Y coordinate on the screen and determine what color is there?
Printable View
How can i specify a X and Y coordinate on the screen and determine what color is there?
VB Code:
Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Now, if you want the RGB values, use this function:
VB Code:
Sub obtainRGB(cValue, varRed, varGreen, varBlue) varBlue = Int(cValue / 65536) cValue = cValue Mod 65536 varGreen = Int(cValue / 256) cValue = cValue Mod 256 varRed = cValue End Sub
Pass the value you get from the GetPixel function, then the variable to store the red value in, then green variable, then blue.
Hope this helps,
cjqp
Probably a bunch of APIs... I guess you'll need the following APIs :
GetForegroundWindow or GetActiveWindow
GetDC
GetPixel
I don't mean to sound rude but, did you actually read his post? He wants to know how to get a color from a pixel, not from a certain window.Quote:
Originally posted by manavo11
Probably a bunch of APIs... I guess you'll need the following APIs :
GetForegroundWindow or GetActiveWindow
GetDC
GetPixel
cjqp
I read it... I've never tried that though so I don't know exactly what is needed... I don't mean to sound rude either, but with the GetPixel API you posted, what is supposed to be passed as the hdc? That's why I thought of the GetActiveWindow API... On second thought maybe the GetDesktopWindow would work...
Ahhh, right, forgot about needing the hDC, yeah, I'd go with GetDesktopWindow for that.
cjqp
VB Code:
Option Explicit Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _ ByVal x As Long, _ ByVal y As Long) _ As Long Private Type POINTAPI x As Long y As Long End Type Private Type RGBA r As Byte g As Byte b As Byte a As Byte End Type Private Type RGBL rgb As Long End Type Private Sub Timer1_Timer() Dim clr As Long Dim rgb As RGBA Dim t As RGBL Dim p As POINTAPI Call GetCursorPos(p) clr = GetPixel(GetWindowDC(0&), p.x, p.y) t.rgb = clr LSet rgb = t Me.BackColor = clr Me.Caption = "R:" & rgb.r & ", G:" & rgb.g & ", B:" & rgb.b End Sub
Why do you declare a?
VB Code:
Private Type RGBA r As Byte g As Byte b As Byte a As Byte End Type
Why not?
Helps me to better remember what's what.
But what does it do? From what I know, R is for Red, G for Green and B for Blue... A is for what?
A is for the alpha.
Its the alpha bit.. Ha ha, get it? :lol: :rolleyes:
*rolls eyes*, anyway, blade, where in your code does it separate the values??
cjqp
Right here...
Code:t.rgb = clr
LSet rgb = t
But how the heck does that split? There is no Property Let or the like that would split it.
cjqp
In VB, you can exchange data in compatible UDTs. A Long is 4 bytes, so if you have one Type with a Long, and one with 4 Bytes, you can break that Long into its individual Bytes.Quote:
Originally posted by cjqp
But how the heck does that split? There is no Property Let or the like that would split it.
cjqp
Red byte
Green byte
Blue byte
Alpha byte
See it?
:)
ahhh, tyvm. Up until your example, my code was by far the shortest for this type of thing.
cjqp
No :lol: :rolleyes:Quote:
Originally posted by crptcblade
Ha ha, get it? :lol: :rolleyes:
I never knew of the Alpha part :D
I suppose technically you don't need it as part of the Type, since it should still just copy over the first 3 bytes anyway, but again, its good to know what's what.
OK. Good to know... I think...Quote:
Originally posted by crptcblade
I suppose technically you don't need it as part of the Type, since it should still just copy over the first 3 bytes anyway, but again, its good to know what's what.