现在的位置: 首页 > 综合 > 正文

VC获取屏幕上任意点的颜色

2018年01月31日 ⁄ 综合 ⁄ 共 853字 ⁄ 字号 评论关闭

项目:  http://download.csdn.net/detail/a379039233/7869757

本来在MouseMove事件中获得鼠标位置及位置,发现只能获取窗体内的颜色,后来参看了其他人的代码才发现

可以在定时器中获取。

主要代码如下:

void CGetColorDlg::OnTimer(UINT nIDEvent) 

{

CPoint point;

GetCursorPos(&point);

HDC hDC = ::GetDC(NULL);

    COLORREF colorref = ::GetPixel(hDC, point.x, point.y);//Get the cursor color

    ::ReleaseDC(NULL,hDC);

//显示鼠标出坐标

CString str;

str.Format("%d,%d",point.x,point.y);

m_cS1.SetWindowText(str);

//填充颜色

CClientDC dc(this);

CRect rc;

m_cG2.GetWindowRect(&rc);

ScreenToClient(rc);

CBrush brush;

brush.Detach(); 

brush.CreateSolidBrush(colorref);

dc.FillRect(rc,&brush);

//RGB值显示

str.Format("%d,%d,%d",colorref&0xFF,(colorref>>8)&0xFF,colorref>>16);

m_cS2.SetWindowText(str);

//RGB Hex值显示

str.Format("#%02X%02X%02X",colorref&0xFF,(colorref>>8)&0xFF,colorref>>16);

m_cS3.SetWindowText(str);

CDialog::OnTimer(nIDEvent);

}

参考文档:

VC_实时获取鼠标指针坐标编程方法 (http://www.docin.com/p-65104153.html)

抱歉!评论已关闭.