Click here to Skip to main content
13,091,363 members (60,357 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 18 Sep 2003

ColorFinder - Retrieve the color of any pixel on the Desktop

, 30 Sep 2003
Rate this:
Please Sign up or sign in to vote.
This article discusses the ColorFinder application that can be used to retrieve the color of any pixel on the desktop in various formats


When developing web-pages or graphics we frequently need a tool that will give us the RGB value of the color of a particular region in some other application's window. An example could be that you need to know the background color of a JPG image so that you can use the same color for the background of the webpage on which you will display the image. ColorFinder comes in handy exactly for this. You can use this tool to pick color from any pixel on the desktop.

How to use the tool

To get the color value left click on the dropper icon and while keeping the left mouse button pressed down move it over the desktop. As the mouse moves, ColorFinder shows the color under the mouse pointer and at the same time it displays the pointer coordinates, value of the color in decimal, hex, and HTML color format. When the mouse pointer is over the color you want to select, release the mouse button. Then using the buttons labeled ">" on the right of any of the text-boxes displaying the color value, copy that value to the clipboard.

By default the tool is always on top of all the application. This allows you to see the color under the mouse pointer as you move the pointer over the desktop. You can disable this behavior by unchecking the "Always on top" menu option in the application's system menu.

Using the code

The source files CButtonSK.h, CButtonSK.cpp, CUrl.h, Curl.cpp are not directly relevant for this application. CButtonSK is used for skinning buttons and are used for the copy buttons (labeled with '>'). Curl is used in the About box to display URLs that behave identically to the URLs inside a browser.

The class associated with the main dialog is CColorFinderDlg and it is defined and implemented in the files ColorFinderDlg.h and ColorFinderDlg.cpp

In the constructor for this class the icon and cursor for the dropper is loaded and the variable m_bTracking is set to false. The current color m_colCurrent is set to black

CColorFinderDlg::CColorFinderDlg(CWnd* pParent /*=NULL*/)
: CDialog(CColorFinderDlg::IDD, pParent)
    m_bTracking    = false;
    m_hCursor      = AfxGetApp()->LoadCursor(IDC_DROPPER);
    m_hDropperIcon = AfxGetApp()->LoadIcon(IDI_DROPPER);
    m_colCurrent   = RGB(0,0,0);

To get the color three Windows messages WM_MOUSEMOVE, WM_LBUTTONDOWN and WM_LBUTTONUP have been used.

When the message handler for WM_LBUTTONDOWN is called, the handler verifies that the location of the mouse button down is inside the rectangle for the static control that displays the dropper icon. It then sets the mouse tracking flag m_bTracking and captures the mouse so that all subsequent mouse move messages are sent to the window for CColorFinderDlg. The handler also changes the mouse cursor to that of the dropper and hides the dropper icon on the dialog. This gives an impression that the dropper is picked up from the dialog's window.

void CColorFinderDlg::OnLButtonDown(UINT nFlags, CPoint point) 
    CRect rect;
    if (PtInRect(&rect, point))
        m_bTracking = true;

The handler for the WM_MOUSEMOVE first verifies that the mouse is being tracked, i.e., the mouse left button is currently down. It does that using the m_bTracking variable set in OnLButtonDown. The current mouse pointer location is obtained using the Win32 API GetCursorPos. To get the color under the mouse pointer first a handle to the desktop device context is obtained using the API GetDCEx . This handle is then converted to a CDC class pointer using CDC::FromHandle. Then color is retrieved using the CDC method GetPixel.

void CColorFinderDlg::OnMouseMove(UINT nFlags, CPoint point) 
    if (m_bTracking)
    CDialog::OnMouseMove(nFlags, point);

void CColorFinderDlg::UpdateColor()
    //  Get the mouse pointer position and display it
    str.Format ("%d, %d", pt.x, pt.y);
    //  Get the device context of the desktop and from it get the color 
    //  of the pixel at the current mouse pointer position
    CDC *pDesktopDC = CDC::FromHandle ( ::GetDCEx(NULL, NULL, 0));
    m_colCurrent = pDesktopDC->GetPixel(pt);

    //  Break the color into the RGB components
    BYTE rVal = GetRValue(m_colCurrent);
    BYTE gVal = GetGValue(m_colCurrent);
    BYTE bVal = GetBValue(m_colCurrent);
    //  Display the pixel color in different formats
    str.Format("%02X, %02X, %02X", rVal, gVal, bVal);
    str.Format("%d, %d, %d", rVal, gVal, bVal);
    str.Format("#%02X%02X%02X", rVal, gVal, bVal);
    //  Show the color in the static control
    CDC *pSDC = m_statCol.GetDC();
    CRect sRect;
    pSDC->FillSolidRect(&sRect, m_colCurrent);

In the handler for the WM_LBUTTONUP the mouse capture is released and the m_bTracking flag is cleared. The dropper icon on the dialog is also restored.

void CColorFinderDlg::OnLButtonUp(UINT nFlags, CPoint point) 
    if (m_bTracking)
        m_bTracking = false;

Points of Interest

The color displayed in the m_statCol is not updated with WM_PAINT message.

The tool gives the actual RGB value of the pixel on the desktop. If the color depth on your desktop is less than 24-bit then all 24-bit colors will be displayed after approximating to the nearest supported color. ColorFinder will display the approximated color and NOT the actual color.


  • v1.0 This is the initial version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I just love coding. I started programming in 1995 with BASIC and then moved through Cobol, Pascal, Prolog, C, C++, VB, VC++ and now C#/.NET.

I received a Bachelor of Technology degree in Computer Science from University of Calcutta in 2001.

I worked for some time in Texas Instruments, Adobe Systems and now in Microsoft India Development Center in the Visual Studio Team Systems.

I am from the City of Joy, Kolkata in India, but now live and code Hyderabad.

You may also be interested in...

Comments and Discussions

GeneralUse Regional of interest Pin
Bahgia Minhat7-Apr-08 5:07
memberBahgia Minhat7-Apr-08 5:07 
uday nedunuri20-Jun-07 0:33
memberuday nedunuri20-Jun-07 0:33 
Question24 RGB BIT Pin
ñoqui9-Apr-06 16:34
memberñoqui9-Apr-06 16:34 
GeneralPicking Color Pin
nelsonf5-Jan-04 17:04
membernelsonf5-Jan-04 17:04 
GeneralRe: Picking Color Pin
DaTxomin28-Oct-05 4:36
memberDaTxomin28-Oct-05 4:36 
GeneralColorFinder Pin
Kawas2-Oct-03 9:51
memberKawas2-Oct-03 9:51 
GeneralHandy Pin
Obliterator2-Oct-03 3:38
memberObliterator2-Oct-03 3:38 
GeneralRe: Handy Pin
abhinaba2-Oct-03 20:30
memberabhinaba2-Oct-03 20:30 
AORD23-Sep-03 13:05
memberAORD23-Sep-03 13:05 
GeneralRe: COLORREF Pin
abhinaba25-Sep-03 20:48
memberabhinaba25-Sep-03 20:48 
GeneralRe: COLORREF Pin
AORD25-Sep-03 21:56
memberAORD25-Sep-03 21:56 
GeneralRe: COLORREF Pin
abhinaba25-Sep-03 22:20
memberabhinaba25-Sep-03 22:20 
GeneralNeat Tool But.. Pin
AORD22-Sep-03 22:44
memberAORD22-Sep-03 22:44 
GeneralRe: Neat Tool But.. Pin
abhinaba23-Sep-03 1:38
memberabhinaba23-Sep-03 1:38 
GeneralRe: Neat Tool But.. Pin
AORD23-Sep-03 12:09
memberAORD23-Sep-03 12:09 
GeneralRe: Neat Tool But.. Pin
abhinaba25-Sep-03 20:45
memberabhinaba25-Sep-03 20:45 
GeneralRe: Neat Tool But.. Pin
AORD26-Sep-03 9:54
memberAORD26-Sep-03 9:54 
GeneralRe: Neat Tool But.. Pin
lvidaguren2-Oct-03 19:10
memberlvidaguren2-Oct-03 19:10 
GeneralRe: Neat Tool But.. Pin
abhinaba2-Oct-03 20:29
memberabhinaba2-Oct-03 20:29 
GeneralRe: Neat Tool But.. Pin
AORD3-Oct-03 1:03
memberAORD3-Oct-03 1:03 
GeneralRe: Neat Tool But.. Pin
abhinaba3-Oct-03 2:02
memberabhinaba3-Oct-03 2:02 
GeneralRe: Neat Tool But.. Pin
AORD3-Oct-03 10:04
memberAORD3-Oct-03 10:04 
GeneralMy Opinion... Pin
Brian (Korea)23-May-06 16:48
memberBrian (Korea)23-May-06 16:48 
QuestionColorFinder Pin
uday nedunuri20-Jun-07 4:04
memberuday nedunuri20-Jun-07 4:04 
GeneralYou did it again!! Pin
WREY18-Sep-03 19:32
memberWREY18-Sep-03 19:32 
GeneralNice and useful :-) Pin
Nishant S18-Sep-03 18:49
editorNishant S18-Sep-03 18:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 1 Oct 2003
Article Copyright 2003 by abhinaba
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid