Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use ClientDC::GetPixel to get the pixel's RGB value when I call TextOut?

How to use ClientDC::GetPixel to get the pixel's RGB value when I call TextOut?

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wangningyu
    wrote on last edited by
    #1

    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 !

    CPalliniC 1 Reply Last reply
    0
    • W wangningyu

      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 !

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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]

      In testa che avete, signor di Ceprano?

      W 1 Reply Last reply
      0
      • CPalliniC CPallini

        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]

        W Offline
        W Offline
        wangningyu
        wrote on last edited by
        #3

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

        CPalliniC 1 Reply Last reply
        0
        • W wangningyu

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

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups