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. Drawing a text over a picture control

Drawing a text over a picture control

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
8 Posts 5 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.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    Hi, I added a picture control to my form and choose the BitMap type and loaded a bitmap into that control. Now I need to draw text over that bitmap. The problem is that the text is being drawn behind that bitmap and not above it(therefor - I can't see the text). I use the following peace of code :

    HBRUSH Frm_WkAtol::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO:  Change any attributes of the DC here
    CRect rect;
    CRgn  rgn;
    switch(pWnd->GetDlgCtrlID())
    {
    	case IDC\_ATOL\_CORRIDOR\_BITMAP:
    	{
    		CRect TextRect;
    		CDC \*pLabelDC = m\_AtolCorridorBitmap.GetDC();
    		m\_AtolCorridorBitmap.GetClientRect(&rect);
    		TextRect = rect;
    		TextRect.top = rect.top + 10;
    		TextRect.bottom = TextRect.top + 25;
    		TextRect.left = rect.left + 25;
    		TextRect.right = rect.right - 25;
    		pLabelDC->DrawText(\_T("Test") , &TextRect , DT\_CENTER);
    		ReleaseDC(pLabelDC);
    	}
    	break;
    }
    
    return hbr;
    

    }

    Note - if I change the type of the picture control to a Rectangle,I can see the text. Doe's it mean that I need to draw the Bitmap by myself??? With best regards, Eli

    S M T H 4 Replies Last reply
    0
    • E eli15021979

      Hi, I added a picture control to my form and choose the BitMap type and loaded a bitmap into that control. Now I need to draw text over that bitmap. The problem is that the text is being drawn behind that bitmap and not above it(therefor - I can't see the text). I use the following peace of code :

      HBRUSH Frm_WkAtol::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
      {
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

      // TODO:  Change any attributes of the DC here
      CRect rect;
      CRgn  rgn;
      switch(pWnd->GetDlgCtrlID())
      {
      	case IDC\_ATOL\_CORRIDOR\_BITMAP:
      	{
      		CRect TextRect;
      		CDC \*pLabelDC = m\_AtolCorridorBitmap.GetDC();
      		m\_AtolCorridorBitmap.GetClientRect(&rect);
      		TextRect = rect;
      		TextRect.top = rect.top + 10;
      		TextRect.bottom = TextRect.top + 25;
      		TextRect.left = rect.left + 25;
      		TextRect.right = rect.right - 25;
      		pLabelDC->DrawText(\_T("Test") , &TextRect , DT\_CENTER);
      		ReleaseDC(pLabelDC);
      	}
      	break;
      }
      
      return hbr;
      

      }

      Note - if I change the type of the picture control to a Rectangle,I can see the text. Doe's it mean that I need to draw the Bitmap by myself??? With best regards, Eli

      S Offline
      S Offline
      shiraztk
      wrote on last edited by
      #2

      Hi Just a guess CCleintDC dc(this) CRect TextRect; m_AtolCorridorBitmap.GetClientRect(&rect); TextRect = rect; TextRect.top = rect.top + 10; TextRect.bottom = TextRect.top + 25; TextRect.left = rect.left + 25; TextRect.right = rect.right - 25; dc.DrawText(_T("Test") , &TextRect , DT_CENTER); Regards

      The Best Religion is Science. Once you understand it, you will know God.

      1 Reply Last reply
      0
      • E eli15021979

        Hi, I added a picture control to my form and choose the BitMap type and loaded a bitmap into that control. Now I need to draw text over that bitmap. The problem is that the text is being drawn behind that bitmap and not above it(therefor - I can't see the text). I use the following peace of code :

        HBRUSH Frm_WkAtol::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
        {
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

        // TODO:  Change any attributes of the DC here
        CRect rect;
        CRgn  rgn;
        switch(pWnd->GetDlgCtrlID())
        {
        	case IDC\_ATOL\_CORRIDOR\_BITMAP:
        	{
        		CRect TextRect;
        		CDC \*pLabelDC = m\_AtolCorridorBitmap.GetDC();
        		m\_AtolCorridorBitmap.GetClientRect(&rect);
        		TextRect = rect;
        		TextRect.top = rect.top + 10;
        		TextRect.bottom = TextRect.top + 25;
        		TextRect.left = rect.left + 25;
        		TextRect.right = rect.right - 25;
        		pLabelDC->DrawText(\_T("Test") , &TextRect , DT\_CENTER);
        		ReleaseDC(pLabelDC);
        	}
        	break;
        }
        
        return hbr;
        

        }

        Note - if I change the type of the picture control to a Rectangle,I can see the text. Doe's it mean that I need to draw the Bitmap by myself??? With best regards, Eli

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        OnCtlColor() is called before the control draws itself so if you draw text there it will be drawn "under" the control, as you are seeing. You could derive your own picture box class from CStatic and make your m_AtolCorridorBitmap a variable of that type. add a WM_PAINT handler to the class. In the OnPaint() implementation, after calling the base class' OnPaint(), draw your text in the client area of the picture box. Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        E 1 Reply Last reply
        0
        • E eli15021979

          Hi, I added a picture control to my form and choose the BitMap type and loaded a bitmap into that control. Now I need to draw text over that bitmap. The problem is that the text is being drawn behind that bitmap and not above it(therefor - I can't see the text). I use the following peace of code :

          HBRUSH Frm_WkAtol::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
          {
          HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

          // TODO:  Change any attributes of the DC here
          CRect rect;
          CRgn  rgn;
          switch(pWnd->GetDlgCtrlID())
          {
          	case IDC\_ATOL\_CORRIDOR\_BITMAP:
          	{
          		CRect TextRect;
          		CDC \*pLabelDC = m\_AtolCorridorBitmap.GetDC();
          		m\_AtolCorridorBitmap.GetClientRect(&rect);
          		TextRect = rect;
          		TextRect.top = rect.top + 10;
          		TextRect.bottom = TextRect.top + 25;
          		TextRect.left = rect.left + 25;
          		TextRect.right = rect.right - 25;
          		pLabelDC->DrawText(\_T("Test") , &TextRect , DT\_CENTER);
          		ReleaseDC(pLabelDC);
          	}
          	break;
          }
          
          return hbr;
          

          }

          Note - if I change the type of the picture control to a Rectangle,I can see the text. Doe's it mean that I need to draw the Bitmap by myself??? With best regards, Eli

          T Offline
          T Offline
          tyagineha
          wrote on last edited by
          #4

          hye eli, can u tell me how to insert picture control.......... NT

          H 1 Reply Last reply
          0
          • E eli15021979

            Hi, I added a picture control to my form and choose the BitMap type and loaded a bitmap into that control. Now I need to draw text over that bitmap. The problem is that the text is being drawn behind that bitmap and not above it(therefor - I can't see the text). I use the following peace of code :

            HBRUSH Frm_WkAtol::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
            {
            HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

            // TODO:  Change any attributes of the DC here
            CRect rect;
            CRgn  rgn;
            switch(pWnd->GetDlgCtrlID())
            {
            	case IDC\_ATOL\_CORRIDOR\_BITMAP:
            	{
            		CRect TextRect;
            		CDC \*pLabelDC = m\_AtolCorridorBitmap.GetDC();
            		m\_AtolCorridorBitmap.GetClientRect(&rect);
            		TextRect = rect;
            		TextRect.top = rect.top + 10;
            		TextRect.bottom = TextRect.top + 25;
            		TextRect.left = rect.left + 25;
            		TextRect.right = rect.right - 25;
            		pLabelDC->DrawText(\_T("Test") , &TextRect , DT\_CENTER);
            		ReleaseDC(pLabelDC);
            	}
            	break;
            }
            
            return hbr;
            

            }

            Note - if I change the type of the picture control to a Rectangle,I can see the text. Doe's it mean that I need to draw the Bitmap by myself??? With best regards, Eli

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            Did you try on the WM_PAINT or WM_ERASEBKGND whats result?


            WhiteSky


            1 Reply Last reply
            0
            • T tyagineha

              hye eli, can u tell me how to insert picture control.......... NT

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              You must insert a static control of Toolbox and set bitmap property on the property of control and use of SetBitmap() for insert image file to this control.


              WhiteSky


              1 Reply Last reply
              0
              • M Mark Salsbery

                OnCtlColor() is called before the control draws itself so if you draw text there it will be drawn "under" the control, as you are seeing. You could derive your own picture box class from CStatic and make your m_AtolCorridorBitmap a variable of that type. add a WM_PAINT handler to the class. In the OnPaint() implementation, after calling the base class' OnPaint(), draw your text in the client area of the picture box. Mark

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                E Offline
                E Offline
                eli15021979
                wrote on last edited by
                #7

                Hi Mark, Thanks for your detailed explanation.

                Mark Salsbery wrote:

                OnCtlColor() is called before the control draws itself so if you draw text there it will be drawn "under" the control, as you are seeing.

                I didn't knew that....:doh:(do you know where can I find a tutorial about the messages and windows events sequence?)

                Mark Salsbery wrote:

                You could derive your own picture box class from CStatic and make your m_AtolCorridorBitmap a variable of that type. add a WM_PAINT handler to the class. In the OnPaint() implementation, after calling the base class' OnPaint(), draw your text in the client area of the picture box.

                This is just what I'm going to try.... Just one more question: In design time I can attach a bitmap resource to the picture control. How can I do it during run time(should I draw the bitmap by myself,or maybe attach the bitmap in design time and only change the declaration of that control , i.e will it save the properties I gave it in design time??) Thanks again, Eli

                M 1 Reply Last reply
                0
                • E eli15021979

                  Hi Mark, Thanks for your detailed explanation.

                  Mark Salsbery wrote:

                  OnCtlColor() is called before the control draws itself so if you draw text there it will be drawn "under" the control, as you are seeing.

                  I didn't knew that....:doh:(do you know where can I find a tutorial about the messages and windows events sequence?)

                  Mark Salsbery wrote:

                  You could derive your own picture box class from CStatic and make your m_AtolCorridorBitmap a variable of that type. add a WM_PAINT handler to the class. In the OnPaint() implementation, after calling the base class' OnPaint(), draw your text in the client area of the picture box.

                  This is just what I'm going to try.... Just one more question: In design time I can attach a bitmap resource to the picture control. How can I do it during run time(should I draw the bitmap by myself,or maybe attach the bitmap in design time and only change the declaration of that control , i.e will it save the properties I gave it in design time??) Thanks again, Eli

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  eli15021979 wrote:

                  How can I do it during run time(should I draw the bitmap by myself,or maybe attach the bitmap in design time and only change the declaration of that control ,

                  You can use the CStatic::SetBitmap() method to set the control's bitmap. You'll need the bitmap to set, and how you get that depends on where it is. If it's a resource, the CBitmap class' CBitmap::LoadBitmap() method or the Image::LoadFromResource() method will do it. If it's a JPEG, GIF, BMP, or PNG file, you could use CImage::Load().

                  eli15021979 wrote:

                  do you know where can I find a tutorial about the messages and windows events sequence?)

                  I wish I had specific links but I don't, sorry :) The book "Programming Windows" by Charles Petzold is a classic text for Windows fundamentals. The Platform SDK has details on every Windows message. For example, the WM_CTLCOLORxxx messages state "is sent to the parent window of a button before drawing the button". Cheers! :) Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  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