AGREE chb2000() -------------------------------------------- OnTimer:CPoint pt; GetCursorPos(&pt);HDC hDC = ::GetDC(NULL); COLORREF clr = ::GetPixel(hDC, pt.x, pt.y); int nR = GetRValue(clr); //Red int nG = GetGValue(clr); //Green int nB = GetBValue(clr); //BlueCOLORREF yourColor if (yourColor==clr) ...
http://www.vckbase.com/document/viewdoc.asp?id=403
--------------------------------------------
OnTimer:CPoint pt;
GetCursorPos(&pt);HDC hDC = ::GetDC(NULL);
COLORREF clr = ::GetPixel(hDC, pt.x, pt.y);
int nR = GetRValue(clr); //Red
int nG = GetGValue(clr); //Green
int nB = GetBValue(clr); //BlueCOLORREF yourColor
if (yourColor==clr)
...
然后,调用clr=GetPixel(hScrdc,x,y),就可以获得指定位置(x,y)处的RGB颜色
再用GetRValue(clr),GetGValure(clr),GetBValue(clr)分别获得R,G,B值。要想获得屏幕上任意矩形区域中的图片,可以用BitBlt()函数。
{
// TODO: Add your message handler code here and/or call default
CRect rc;
GetDlgItem( IDC_STATIC_PALETTE )->GetWindowRect( &rc );
ScreenToClient( &rc );
if( rc.PtInRect( point ) )
{
CClientDC dc(this);
UpdateColor( dc.GetPixel( point ) );//得到颜色的就是这一句
}
CInitDialogBar::OnLButtonDown(nFlags, point);
}void CToolColorSelect::UpdateColor( COLORREF dwColor )
{
m_btnColor.SetColor( dwColor );
m_nRed = GetRValue( dwColor );
m_nGreen = GetGValue( dwColor );
m_nBlue = GetBValue( dwColor );
UpdateData( FALSE ); ((CSliderCtrl*)GetDlgItem( IDC_SLIDER_VALUER ))->SetPos(m_nRed);
((CSliderCtrl*)GetDlgItem( IDC_SLIDER_VALUEG ))->SetPos(m_nGreen);
((CSliderCtrl*)GetDlgItem( IDC_SLIDER_VALUEB ))->SetPos(m_nBlue); GlobalPenSet * pPenSet;
pPenSet=((CPatternDesignCADApp *)AfxGetApp())->GetGlobalPenSetPtr();
if(dwColor==SYS_COLOR_WHITE)
dwColor=RGB(254,254,254);
if(dwColor==SYS_COLOR_BLACK)
dwColor=RGB(1,1,1);
pPenSet->SetPenColor(dwColor);
}
int nLineLen;
BITMAPINFOHEADER bi; CDibDoc* pDoc = GetDocument();
HDIB hdib = pDoc->GetHDIB();
if (hdib == NULL)
return;// ClientToDoc(lgcPoint); //当前图象中获得数据
DibInfo (hdib, &bi);
nWidth = (int)bi.biWidth;
nHeight = (int)bi.biHeight; if(bi.biBitCount != 24) //只能处理24位图像
return ;
ptr = (BYTE *)GlobalLock(hdib);
ptr += (DWORD)bi.biSize + (DWORD)PaletteSize((LPSTR)&bi);
//每行数据的字节数
int zhang1 = 1;
zhang1 = ( nWidth * 3 + 3 ) / 4 ;
nLineLen = zhang1 * 4; for(int y = 0 ; y < nHeight ; y++)
for(int x = 0 ; x < nWidth ; x++)
{
int B = *(ptr + (DWORD)(nHeight - y - 1) * nLineLen + x*3);
int G = *(ptr + (DWORD)(nHeight - y - 1) * nLineLen + x*3+1);
int R = *(ptr + (DWORD)(nHeight - y - 1) * nLineLen + x*3+2); bmp_color_r[y][x] = R;
bmp_color_b[y][x] = B;
bmp_color_g[y][x] = G; pellet_label[y][x] = 0;
}看看吧,是处理24位的