Results 1 to 4 of 4

Thread: Using GetPixel() anywhere on the screen.

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    2

    Red face

    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.

  2. #2
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    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

    Code:
    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
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  3. #3
    Guest
    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    2

    Exclamation

    Ok, that code helps some, and I have an idea of what is going on, for the most part. However this segment

    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width