65.9K
CodeProject 正在变化。 阅读更多。
Home

ColorFinder - 获取桌面任何像素的颜色

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (30投票s)

2003年9月19日

3分钟阅读

viewsIcon

139217

downloadIcon

4296

本文讨论了 ColorFinder 应用程序,该应用程序可用于以各种格式检索桌面任何像素的颜色

引言

在开发网页或图形时,我们经常需要一个工具来获取某个应用程序窗口中特定区域的颜色 RGB 值。例如,您可能需要知道 JPG 图像的背景颜色,以便您可以在将显示该图像的网页背景中使用相同的颜色。ColorFinder 正好可以做到这一点。您可以使用此工具从桌面上的任何像素中拾取颜色。

如何使用该工具

要获取颜色值,请左键单击滴管图标 ,并按住鼠标左键在桌面上移动它。随着鼠标的移动,ColorFinder 会显示鼠标指针下的颜色,同时显示指针坐标、颜色的十进制、十六进制和 HTML 颜色格式的值。当鼠标指针位于您要选择的颜色上时,松开鼠标按钮。然后使用显示颜色值的任何文本框右侧标有“>”的按钮,将该值复制到剪贴板。

默认情况下,该工具始终位于所有应用程序的顶部。这使您可以在鼠标指针在桌面上移动时看到鼠标指针下的颜色。您可以通过取消选中应用程序系统菜单中的“始终置于顶层”菜单选项来禁用此行为。

使用代码

源文件 CButtonSK.h, CButtonSK.cpp, CUrl.h, Curl.cpp 与此应用程序不直接相关。CButtonSK 用于对按钮进行美化,用于复制按钮(标有“>”)。Curl 用于“关于”框中,以显示与浏览器内部的 URL 行为完全相同的 URL。

与主对话框关联的类是 CColorFinderDlg,它在文件 ColorFinderDlg.hColorFinderDlg.cpp 中定义和实现

在该类的构造函数中,加载了滴管的图标和光标,并且变量 m_bTracking 设置为 false。 当前颜色 m_colCurrent 设置为黑色

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);
}

为了获取颜色,使用了三个 Windows 消息 WM_MOUSEMOVEWM_LBUTTONDOWNWM_LBUTTONUP

当调用 WM_LBUTTONDOWN 的消息处理程序时,处理程序会验证鼠标按下位置是否在显示滴管图标的静态控件的矩形内。然后它设置鼠标跟踪标志 m_bTracking 并捕获鼠标,以便将所有后续鼠标移动消息发送到 CColorFinderDlg 的窗口。处理程序还将鼠标光标更改为滴管的光标,并隐藏对话框上的滴管图标。这给人一种从对话框窗口中拾取滴管的印象。

void CColorFinderDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CRect rect;
    m_statDropper.GetWindowRect(&rect);
    ClientToScreen(&point);
    if (PtInRect(&rect, point))
    {
        SetCapture();
        m_bTracking = true;
        ::SetCursor(m_hCursor);
        m_statDropper.SetIcon(NULL);
    }
    ...
}

WM_MOUSEMOVE 的处理程序首先验证鼠标是否正在被跟踪,即,鼠标左键当前是否被按下。它使用 OnLButtonDown 中设置的 m_bTracking 变量来执行此操作。当前鼠标指针位置是使用 Win32 API GetCursorPos 获得的。为了获取鼠标指针下的颜色,首先使用 API GetDCEx 获取桌面设备上下文的句柄。然后使用 CDC::FromHandle 将此句柄转换为 CDC 类指针。然后使用 CDC 方法 GetPixel 检索颜色。

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

void CColorFinderDlg::UpdateColor()
{
    ...
    //  Get the mouse pointer position and display it
    GetCursorPos(&pt);
    str.Format ("%d, %d", pt.x, pt.y);
    m_editCurPos.SetWindowText((LPCTSTR)str);
    
    //  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);
    m_editColHex.SetWindowText((LPCTSTR)str);
    
    str.Format("%d, %d, %d", rVal, gVal, bVal);
    m_editColDec.SetWindowText((LPCTSTR)str);
    
    str.Format("#%02X%02X%02X", rVal, gVal, bVal);
    m_editColHTML.SetWindowText((LPCTSTR)str);
    
    //  Show the color in the static control
    CDC *pSDC = m_statCol.GetDC();
    CRect sRect;
    m_statCol.GetClientRect(&sRect);
    pSDC->FillSolidRect(&sRect, m_colCurrent);
    
    ...
}

WM_LBUTTONUP 的处理程序中,释放鼠标捕获,并清除 m_bTracking 标志。对话框上的滴管图标也会被恢复。

void CColorFinderDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
    if (m_bTracking)
    {
        ReleaseCapture();
        m_bTracking = false;
        m_statDropper.SetIcon(m_hDropperIcon);
    }
    ...
}

关注点

m_statCol 中显示的颜色不会使用 WM_PAINT 消息更新。

该工具给出桌面像素的实际 RGB 值。如果您的桌面颜色深度小于 24 位,则所有 24 位颜色都将在近似到最近支持的颜色后显示。ColorFinder 将显示近似颜色,而不是实际颜色。

历史

  • v1.0 这是初始版本。
© . All rights reserved.