A method to get the pixel color under the mouse pointer | CodeGuru

A method to get the pixel color under the mouse pointer

This small code sample shows how to obtain the RGB color of any pixel under the cursor. The code uses a timer control to fire events ever 100’th of a second and then uses the GetPixel, GetCursorPos, and GetDC WinAPI calls to obtain the value. option Explicit ‘ ‘ ‘Aaron Young ‘Analyst Programmer ‘ajyoung@pressenter.com […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 29, 2004
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This small code sample shows how to obtain the RGB color of any pixel under the cursor.

Screen-Shot

The code uses a timer control to fire events ever 100’th of a second and then uses the GetPixel, GetCursorPos, and GetDC WinAPI calls to obtain the value.

option Explicit
'
'
'Aaron Young
'Analyst Programmer
'ajyoung@pressenter.com <mailto:ajyoung@pressenter.com>
'http://www.pressenter.com/~ajyoung
'

'Add a Timer Control to a Form, then use this code And point to
'anywhere on your screen to have
'the RGB value appear In the Forms Caption.
'
private Type POINTAPI
    x as Long
    y as Long
End Type
'
private Declare Function GetPixel Lib "gdi32" (byval hdc as Long, _
    byval x as Long, byval y as Long) as Long
private Declare Function GetCursorPos Lib "user32" _
        (lpPoint as POINTAPI) as Long
private Declare Function GetWindowDC Lib "user32" (byval hwnd as Long) _
        as Long
'
private Sub Form_Load()
    Timer1.Interval = 100
End Sub
'
private Sub Timer1_Timer()
    Dim tPOS as POINTAPI
    Dim sTmp as string
    Dim lColor as Long
    Dim lDC as Long
'
    lDC = GetWindowDC(0)
    Call GetCursorPos(tPOS)
    lColor = GetPixel(lDC, tPOS.x, tPOS.y)
    Label2.BackColor = lColor
'   
    sTmp = Right$("000000" & Hex(lColor), 6)
    Caption = "R:" & Right$(sTmp, 2) & " G:" &_
         mid$(sTmp, 3, 2) & " B:" & Left$(sTmp, 2)
End Sub
'

Download zipped project file

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.