Get Pixel Color at Any Screen Point [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Get Pixel Color at Any Screen Point [Windows]

Post by RASHAD »

Hi all

Code: Select all

Global p.POINT

Procedure GetColor() 
  GetCursorPos_(@p.POINT ) 
  hDC = GetDC_(0) 
  If hDC <> 0 
    Color = GetPixel_(hDC,p\x,p\y) 
    ReleaseDC_(0, hDC) 
    ProcedureReturn Color 
  EndIf 
EndProcedure

OpenWindow(0,0,0,150,120,"",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(0,10,10,180,20,"")
TextGadget(1,10,35,180,20,"")
TextGadget(2,10,60,180,20,"")
TextGadget(3,10,85,180,20,"")

AddWindowTimer(0,125,10)
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Timer  
      color = GetColor()
      SetGadgetText(0,"X : "+Str(p\x)+"  Y :  "+Str(p\y))
      SetGadgetText(1,Str(color))
      SetGadgetText(2,"$"+Hex(color))
      SetGadgetText(3,"RGB("+Str(Red(color))+","+Str(Green(color))+ ","+Str(Blue(color))+")")      
  EndSelect
Until Quit = 1

Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Get Pixel Color at Any Screen Point [Windows]

Post by Kwai chang caine »

Simple and efficient :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Cruster
User
User
Posts: 96
Joined: Fri Jan 23, 2004 12:05 am

Re: Get Pixel Color at Any Screen Point [Windows]

Post by Cruster »

Oh! Very useful :D
PureBasic 4.3 registered user
Post Reply