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. Draw a transparent rectangle

Draw a transparent rectangle

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformancetutorial
9 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.
  • U Offline
    U Offline
    uresh hanmugam
    wrote on last edited by
    #1

    Hi all, At runtime how to draw a transparent rectangle on the bitmap, which is loaded from the file using memory DC. thanks in advance

    L C H 3 Replies Last reply
    0
    • U uresh hanmugam

      Hi all, At runtime how to draw a transparent rectangle on the bitmap, which is loaded from the file using memory DC. thanks in advance

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      surezu wrote:

      draw a transparent rectangle

      Here try the following code: void DrawTransparentRectangle( CDC& dc, Rect rc) { // the next line draws a transparent rectangle // there now can you see it? // i'm sure it is there, look again }

      M 1 Reply Last reply
      0
      • U uresh hanmugam

        Hi all, At runtime how to draw a transparent rectangle on the bitmap, which is loaded from the file using memory DC. thanks in advance

        C Offline
        C Offline
        chandu004
        wrote on last edited by
        #3

        what is the problem u r facing right now? if u r problem is that u r rectangle drawn with Rectangle()masks u r bmp then draw 4 lines simple.

        Suggestion to the members: prefix your main thread subject with [SOLVED] if it is solved. chandu.

        U 1 Reply Last reply
        0
        • U uresh hanmugam

          Hi all, At runtime how to draw a transparent rectangle on the bitmap, which is loaded from the file using memory DC. thanks in advance

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

          See Graphics::DrawRectangle on the MSDN.

          1 Reply Last reply
          0
          • C chandu004

            what is the problem u r facing right now? if u r problem is that u r rectangle drawn with Rectangle()masks u r bmp then draw 4 lines simple.

            Suggestion to the members: prefix your main thread subject with [SOLVED] if it is solved. chandu.

            U Offline
            U Offline
            uresh hanmugam
            wrote on last edited by
            #5

            i'm having a CBitmap object, which will contain 2546x3288 size image,loading from bmp file.i need to draw a rectangle on that image witout lossing of background image... void SFormViewer::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CWnd::OnPaint() for painting messages if(flag_iInitPaint) { CRect rClient; GetClientRect(&rClient); CPen m_Pen; m_Pen.CreatePen(PS_SOLID,2,RGB(250,0,0)); CDC m_memDC; m_memDC.CreateCompatibleDC(&dc); m_memDC.SelectObject(&m_FormImage); m_memDC.SelectObject(&m_Pen); m_memDC.SetBkMode(TRANSPARENT); r_SelectedRect.SetRect(m_iShownImgTopX + p_iTopLeft.x, m_iShownImgTopY + p_iTopLeft.y, m_iShownImgTopX + p_iBottomRight.x, m_iShownImgTopY + p_iBottomRight.y); if(flag_Scroll == 0) { m_memDC.Rectangle(r_SelectedRect); } dc.BitBlt(0,0,rClient.Width(),rClient.Height(),&m_memDC,m_iShownImgTopX,m_iShownImgTopY,SRCCOPY); } }

            C M 2 Replies Last reply
            0
            • U uresh hanmugam

              i'm having a CBitmap object, which will contain 2546x3288 size image,loading from bmp file.i need to draw a rectangle on that image witout lossing of background image... void SFormViewer::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CWnd::OnPaint() for painting messages if(flag_iInitPaint) { CRect rClient; GetClientRect(&rClient); CPen m_Pen; m_Pen.CreatePen(PS_SOLID,2,RGB(250,0,0)); CDC m_memDC; m_memDC.CreateCompatibleDC(&dc); m_memDC.SelectObject(&m_FormImage); m_memDC.SelectObject(&m_Pen); m_memDC.SetBkMode(TRANSPARENT); r_SelectedRect.SetRect(m_iShownImgTopX + p_iTopLeft.x, m_iShownImgTopY + p_iTopLeft.y, m_iShownImgTopX + p_iBottomRight.x, m_iShownImgTopY + p_iBottomRight.y); if(flag_Scroll == 0) { m_memDC.Rectangle(r_SelectedRect); } dc.BitBlt(0,0,rClient.Width(),rClient.Height(),&m_memDC,m_iShownImgTopX,m_iShownImgTopY,SRCCOPY); } }

              C Offline
              C Offline
              chandu004
              wrote on last edited by
              #6

              surezu wrote:

              r_SelectedRect.SetRect(m_iShownImgTopX + p_iTopLeft.x, m_iShownImgTopY + p_iTopLeft.y, m_iShownImgTopX + p_iBottomRight.x, m_iShownImgTopY + p_iBottomRight.y); if(flag_Scroll == 0) { m_memDC.Rectangle(r_SelectedRect); }

              what i suggest is, instead of using memDC.rectangle, use like this. say if you want to draw rectangle from (x1,y1) to (x2,y2) moveto(x1,y1); lineto(x2,y1); moveto(x2,y1); lineto(x2,y2); moveto(x2,y2); lineto(x1,y2); try to guess the next part of the code.

              Suggestion to the members: prefix your main thread subject with [SOLVED] if it is solved. chandu.

              M 1 Reply Last reply
              0
              • C chandu004

                surezu wrote:

                r_SelectedRect.SetRect(m_iShownImgTopX + p_iTopLeft.x, m_iShownImgTopY + p_iTopLeft.y, m_iShownImgTopX + p_iBottomRight.x, m_iShownImgTopY + p_iBottomRight.y); if(flag_Scroll == 0) { m_memDC.Rectangle(r_SelectedRect); }

                what i suggest is, instead of using memDC.rectangle, use like this. say if you want to draw rectangle from (x1,y1) to (x2,y2) moveto(x1,y1); lineto(x2,y1); moveto(x2,y1); lineto(x2,y2); moveto(x2,y2); lineto(x1,y2); try to guess the next part of the code.

                Suggestion to the members: prefix your main thread subject with [SOLVED] if it is solved. chandu.

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

                chandu004 wrote:

                what i suggest is, instead of using memDC.rectangle,

                Why is 8 lines of code better than 1? Inquiring minds want to know... Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • L led mike

                  surezu wrote:

                  draw a transparent rectangle

                  Here try the following code: void DrawTransparentRectangle( CDC& dc, Rect rc) { // the next line draws a transparent rectangle // there now can you see it? // i'm sure it is there, look again }

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

                  Ha! I can see right through your reply :)

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • U uresh hanmugam

                    i'm having a CBitmap object, which will contain 2546x3288 size image,loading from bmp file.i need to draw a rectangle on that image witout lossing of background image... void SFormViewer::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CWnd::OnPaint() for painting messages if(flag_iInitPaint) { CRect rClient; GetClientRect(&rClient); CPen m_Pen; m_Pen.CreatePen(PS_SOLID,2,RGB(250,0,0)); CDC m_memDC; m_memDC.CreateCompatibleDC(&dc); m_memDC.SelectObject(&m_FormImage); m_memDC.SelectObject(&m_Pen); m_memDC.SetBkMode(TRANSPARENT); r_SelectedRect.SetRect(m_iShownImgTopX + p_iTopLeft.x, m_iShownImgTopY + p_iTopLeft.y, m_iShownImgTopX + p_iBottomRight.x, m_iShownImgTopY + p_iBottomRight.y); if(flag_Scroll == 0) { m_memDC.Rectangle(r_SelectedRect); } dc.BitBlt(0,0,rClient.Width(),rClient.Height(),&m_memDC,m_iShownImgTopX,m_iShownImgTopY,SRCCOPY); } }

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

                    Rectangle draws a rectangle filled with the current selected brush. Try selecting a null/hollow brush into the memDC instead of setting the background mode to transparent CBrush HollowBrush; HollowBrush.CreateStockObject(HOLLOW_BRUSH); CBrush *pOldBrush = m_memDC.SelectObject(&HollowBrush); ... <draw your rectangle> ... m_memDC.SelectObject(pOldBrush); It's also a good idea to get into the habit of saving objects previously selected into DCs so you can select them back in when you're done with the DC.  This will prevent resource leak problems. Also....what happens if the bitmap isn't compatible with the memDC?? Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    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