How to use ClientDC::GetPixel to get the pixel's RGB value when I call TextOut?
-
Hello ! I debug along time.but isn't success! it isn't output "Call GetPixel Failed !", but isn't goto the other judgment. the code like this:
class CTestDlg : public CDialog
{
public:
// ......public:
void TextOutPut(CString str,
HFONT hNew,
HFONT &hOld,
SIZE &szt)
{
CClientDC dc(this);// Set the back and text color GetWindowRect(&rt); dc.SetBkColor(RGB(255,255,255)); dc.SetTextColor(RGB(0,0,0)); dc.SetMapMode(MM\_TEXT); // Use CClientDC::TextOut hOld = (HFONT)::SelectObject(dc,hNew); dc.TextOut(0,0,str); // I can use this function to get the szt's value like 32\*33 GetTextExtentPoint32(dc,str,str.GetLength(),&szt); COLORREF cr; POINT pt; // Now start to find the pixcel's RGB value. for (int i=0; i<szt.cy; i++) { for (int j=0; j<szt.cx; j++) { pt.x = j; pt.y = i; cr = dc.GetPixel(pt); if( -1 == cr ) { TRACE("Call GetPixel Failed !\\r\\n"); } else if( RGB(255,255,255) == cr ) { CString str; str.Format("cur pix position: x=%d y=%d is white.",j,i); TRACE(str); // Do something. } else if( RGB(0,0,0) == cr ) { CString str; str.Format("cur pix position: x=%d y=%d is black.",j,i); TRACE(str); // Do something. } } } }
};
I think this maybe the GetPixel 's parents is wrong. How to solve this problem ? Thanks for your reply ! Best regards !
-
Hello ! I debug along time.but isn't success! it isn't output "Call GetPixel Failed !", but isn't goto the other judgment. the code like this:
class CTestDlg : public CDialog
{
public:
// ......public:
void TextOutPut(CString str,
HFONT hNew,
HFONT &hOld,
SIZE &szt)
{
CClientDC dc(this);// Set the back and text color GetWindowRect(&rt); dc.SetBkColor(RGB(255,255,255)); dc.SetTextColor(RGB(0,0,0)); dc.SetMapMode(MM\_TEXT); // Use CClientDC::TextOut hOld = (HFONT)::SelectObject(dc,hNew); dc.TextOut(0,0,str); // I can use this function to get the szt's value like 32\*33 GetTextExtentPoint32(dc,str,str.GetLength(),&szt); COLORREF cr; POINT pt; // Now start to find the pixcel's RGB value. for (int i=0; i<szt.cy; i++) { for (int j=0; j<szt.cx; j++) { pt.x = j; pt.y = i; cr = dc.GetPixel(pt); if( -1 == cr ) { TRACE("Call GetPixel Failed !\\r\\n"); } else if( RGB(255,255,255) == cr ) { CString str; str.Format("cur pix position: x=%d y=%d is white.",j,i); TRACE(str); // Do something. } else if( RGB(0,0,0) == cr ) { CString str; str.Format("cur pix position: x=%d y=%d is black.",j,i); TRACE(str); // Do something. } } } }
};
I think this maybe the GetPixel 's parents is wrong. How to solve this problem ? Thanks for your reply ! Best regards !
What is the
cr
value? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What is the
cr
value? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]COLORREF cr
maybe the cr is other color's RGB value. but I call this functions,so it doesn't be other value,isn't it ?
dc.SetBkColor(RGB(255,255,255));
dc.SetTextColor(RGB(0,0,0)); -
COLORREF cr
maybe the cr is other color's RGB value. but I call this functions,so it doesn't be other value,isn't it ?
dc.SetBkColor(RGB(255,255,255));
dc.SetTextColor(RGB(0,0,0));Why don't you use the debugger to see what's cr value, at runtime? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]