Author Topic: Get Pixel Color everywhere  (Read 1314 times)

0 Members and 1 Guest are viewing this topic.

Nicola

  • Guest
Get Pixel Color everywhere
« on: October 06, 2020, 06:48:39 AM »
Hi,
A question.
Could I read the color of a pixel outside of a specific window, Without specifying the handle of the specific window or object?

thanks

Charles Pegge

  • Guest
Re: Get Pixel Color everywhere
« Reply #1 on: October 06, 2020, 02:25:10 PM »
I think you get the hDC of the whole screen by setting the Window handle as null:

A C snippet from this page:
https://stackoverflow.com/questions/2659932/how-to-read-the-screen-pixels

Code: [Select]
HDC hdcScreen = ::GetDC(NULL);
COLORREF pixel = ::GetPixel(hdcScreen, x, y);
ReleaseDC(NULL, hdcScreen);

Charles Pegge

  • Guest
Re: Get Pixel Color everywhere
« Reply #2 on: October 07, 2020, 08:28:44 AM »
Unfortunately, there are some very strange offsets when trying to read a screen pixel (using GetPixel). Perhaps you can make sense of it.

Code: [Select]

  uses corewin


  % ID_Timer   3000
  % Interval   100


 
  dim cmdline as asciiz ptr,inst as sys
  &cmdline=GetCommandLine
  inst=GetModuleHandle 0
  '
  '
  function WndProc (sys hWnd,wMsg,wParam,lparam) as sys callback
  ==============================================================

    static as sys hdc, htimer
    static as String txt
    static as RECT crect
    static as PAINTSTRUCT Paintst
    static as POINT p
    static as int pix
 
    '==========
    select wMsg
    '==========
       
      case WM_CREATE

      static sys hDCs=GetDC 0
      hTimer = SetTimer (hWnd, ID_Timer, 100, 0)
      GetClientRect  hWnd,&cRect

       case WM_TIMER

      GetCursorPos @p
      pix=GetPixel(hDCs,p.x, p.y)
      InvalidateRect hwnd,&crect, 1

      case WM_DESTROY
         
      ReleaseDC hDCs
      KillTimer(hWnd,ID_TIMER)
      PostQuitMessage 0
       
      case WM_PAINT

      hDC=BeginPaint hWnd,&Paintst
      GetClientRect  hWnd,&cRect
      'style
      '0x20 DT_SINGLELINE
      '0x04 DT_VCENTER
      '0x01 DT_CENTER
      '0x25

       SetBkColor   hdc,pix
       SetTextColor hdc,0
       txt="x y color " str(p.x) " , " str(p.y) " , " hex(pix,4)
       DrawText hDC,strptr txt,-1,&cRect,0x25
       EndPaint hWnd,&Paintst
       ValidateRect hwnd,&crect
       
      case WM_KEYDOWN

        Select wParam

Case 27 : SendMessage hwnd, WM_CLOSE, 0, 0      'ESCAPE

        End Select
       '
       InvalidateRect hwnd, &cRect, 1 'FULL SCREEN REFRESH

      case else
         
        function=DefWindowProc hWnd,wMsg,wParam,lParam
       
    end select

  end function ' WndProc


  Function WinMain(sys inst,prevInst,zstring*cmdline,sys show) as sys
  ===================================================================

  WndClass wc
  MSG      wm

  sys hwnd, wwd, wht, wtx, wty, tax

  wc.style = CS_HREDRAW or CS_VREDRAW
  wc.lpfnWndProc = @WndProc
  wc.cbClsExtra =0
  wc.cbWndExtra =0   
  wc.hInstance =inst
  wc.hIcon=LoadIcon 0, IDI_APPLICATION
  wc.hCursor=LoadCursor 0,IDC_ARROW
  wc.hbrBackground = GetStockObject WHITE_BRUSH
  wc.lpszMenuName =0
  wc.lpszClassName = strptr "Demo"

  RegisterClass (&wc)
 
  Wwd = 400 : Wht = 200
  Tax = GetSystemMetrics SM_CXSCREEN
  Wtx = (Tax - Wwd) /2
  Tax = GetSystemMetrics SM_CYSCREEN
  Wty = (Tax - Wht) /2
 
  hwnd = CreateWindowEx 0,wc.lpszClassName,"OXYGEN BASIC",WS_OVERLAPPEDWINDOW,Wtx,Wty,Wwd,Wht,0,0,inst,0
  ShowWindow hwnd,SW_SHOW
  UpdateWindow hwnd

  '
  sys bRet
  '
  do while bRet := GetMessage (@wm, 0, 0, 0)
    if bret=0
      exit while
    end if
    if bRet = -1
      'show an error message
      exit while
    else
      TranslateMessage @wm
      DispatchMessage @wm
    end if
  end while
  End Function

  WinMain inst,0,cmdline,SW_NORMAL
« Last Edit: October 07, 2020, 02:58:53 PM by Charles Pegge »

JRS

  • Guest
Re: Get Pixel Color everywhere
« Reply #3 on: October 07, 2020, 10:07:16 PM »
Quote
The x-coordinate, in logical units, of the pixel to be examined.

How does that differ from pixel count position?

Is GetPixel DPI aware?
« Last Edit: October 07, 2020, 11:13:19 PM by John »

Nicola

  • Guest
Re: Get Pixel Color everywhere
« Reply #4 on: October 08, 2020, 11:47:17 PM »
Hi Charles.
Your program works great. Very well done. Although, for me as a beginner at O2, there are so many instructions that are new.

... if I wanted to create an external function, to put in an .inc file, could it be done?

As for the value of the DPI, it seems to me that there may be problems.

However, you are great.  ;)

Charles Pegge

  • Guest
Re: Get Pixel Color everywhere
« Reply #5 on: October 12, 2020, 06:31:19 AM »
Hi Nicola, :)

You can put any source code you like into an inc file. But declared functions for accessing DLLs need to be within an extern block

On rhe other side, when making DLLs, the exported functions need to go in an extern export block, or be individually tagged 'export'

Some DLL demos in examples\DLLs.