Get Pixel Color Outside Your Form

The picture box will be painted with the color of the pixel that found under the mouse cursor (no matter if the mouse cursor is inside or outside your form).
The text box will show the color Long value (or Hex value).

Preparations

Add 1 Timer, 1 Picture Box and 1 Text Box to your form.
Set the timer Interval property to 10.

Module Code

Declare Function CreateDC& Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, _
ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Any)
Declare Function DeleteDC& Lib "gdi32" (ByVal hdc As Long)
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _
ByVal y As Long) As Long

Type POINTAPI
x As Long
y As Long
End Type

Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Form Code

Dim z As POINTAPI

Private Sub Timer1_Timer()
    GetCursorPos z
    screendc = CreateDC("DISPLAY", "", "", 0&)
'replace the line below with:  Text1 = Hex(GetPixel(screendc, z.x, z.y))
'if you want that the text box will show the Hex value of the color (the Hex value is
'used to define colors in HTML)
   
Text1 = GetPixel(screendc, z.x, z.y)
    Picture1.BackColor = GetPixel(screendc, z.x, z.y)
    DeleteDC (screendc)
End Sub

Go Back