PDA

Click to See Complete Forum and Search --> : Using GetPixel() anywhere on the screen.


cvstrat
Oct 8th, 2000, 07:59 PM
Here is what I'm trying to do.

Make GetPixel work anywhere on the screen, not just in a picture box.

Set the RGB color equal to a variable.

It sounds simple but I haven't met anywone yet who could give me to code for it. Help is MUCH appreciated, I have run out of resources I'm afraid.

gwdash
Oct 8th, 2000, 09:18 PM
This didn't work for me, but i don't know much about get pixel, this code gets the hDC of the DesktopWindow, then you can use GetPixel


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

Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Type RGBTRIPLE
rgbtBlue As Byte
rgbtGreen As Byte
rgbtRed As Byte
End Type




Public Function GetPixelColors(PosX As Long, PosY As Long) As RGBTRIPLE
Dim wnd As Long, DC As Long, RGBClr As Long
Dim Red As Single, Green As Single, Blue As Single
wnd = GetDesktopWindow

DC = GetDC(wnd)

RGBClr = GetPixel(DC, PosX, PosY)

Red = RGBClr Mod &H100
Green = Int(RGBClr / &H100) Mod &H100
Blue = Int(RGBClr / &H10000) Mod &H100

GetPixelColors.rgbtBlue = Blue
GetPixelColors.rgbtGreen = Green
GetPixelColors.rgbtRed = Red
End Function

Oct 9th, 2000, 01:52 PM
Sure it will work on anything. Just replace Picture1.hDC with the DC of the Window you want to work with.

If you have the hWnd of the window, you can retrieve its DC by using GetWindowDC if you want the whole window and GetDC if you want the client area.

cvstrat
Oct 9th, 2000, 03:33 PM
Ok, that code helps some, and I have an idea of what is going on, for the most part. However this segment

Red = RGBClr Mod & H100
Green = Int(RGBClr / & H100 ) Mod & H100
Blue = Int(RGBClr / & H100 00) Mod & H100

comes up red and says there is a syntax error.

I pasted every bit of the code given into a module (which may not be how I should do it), and this is the only part that seems to be causing a problem. If someone could help me figure out what pieces go into a module, and what into the formload event, as well as what's causing the syntax error in the above code, that would be great. Keep in mind I'm trying make the program work anywhere on the screen, not just the active window, but whatever happens to be on the screen at the time. I'm am using VB6 if this helps.

Thanks for the help so far.