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. Device context

Device context

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 Posters 1 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.
  • H Offline
    H Offline
    Homber Martinez
    wrote on last edited by
    #1

    What is needed to draw on a device context CDC dc; dc.LineTo(10,10); i'm new to this so how should this peace of code look like ... what am i missing

    P 1 Reply Last reply
    0
    • H Homber Martinez

      What is needed to draw on a device context CDC dc; dc.LineTo(10,10); i'm new to this so how should this peace of code look like ... what am i missing

      P Offline
      P Offline
      Paul Wolfensberger
      wrote on last edited by
      #2

      Well where are you getting the DC from?? Typically if you use an override of the virtual CView::OnDraw(CDC* pDC) method, the framework provides you the device context. From there you simply use it! CMyView::OnDraw(CDC* pDC) { pDC->MoveTo(50, 50); // Line start pDC->LineTo(10, 10); // Line end pDC->Ellipse(0,0,100,100); // Draw a circle.

      H 1 Reply Last reply
      0
      • P Paul Wolfensberger

        Well where are you getting the DC from?? Typically if you use an override of the virtual CView::OnDraw(CDC* pDC) method, the framework provides you the device context. From there you simply use it! CMyView::OnDraw(CDC* pDC) { pDC->MoveTo(50, 50); // Line start pDC->LineTo(10, 10); // Line end pDC->Ellipse(0,0,100,100); // Draw a circle.

        H Offline
        H Offline
        Homber Martinez
        wrote on last edited by
        #3

        isn't it possible to create my own device context to play around with ... in any member function void MyClass::myfunc(int i,int a) { CDC dc; dc.LineTo(10,10); }

        C U 2 Replies Last reply
        0
        • H Homber Martinez

          isn't it possible to create my own device context to play around with ... in any member function void MyClass::myfunc(int i,int a) { CDC dc; dc.LineTo(10,10); }

          C Offline
          C Offline
          Cristi Posea
          wrote on last edited by
          #4

          Usually, you will want to draw in a window's device context. As Paul said above, you will get such a DC in handlers like OnPaint() or in OnDraw() for views. If you have a window, you can get the window's DC using GetDC(), draw into it, then use ReleaseDC(). As a rule of thumb, at least for the beginning, try to keep the painting stuf in OnDraw(). You can call from there helper functions, giving them a pointer to the provided DC (pDC is fine). Also, you can create a "memory DC", which basically behaves like a normal DC, but you will not see the results. However, you can copy (blit) the contents of such a DC into a normal window DC. Also, you can use a memory DC to draw on a bitmap. For example: CDC dc; dc.CreateCompatibleDC(NULL); // creates a mem DC compatible with the display. ... // paint here, etc. dc.DeleteDC(); Other device context are related to printers, metafiles, etc.

          H 1 Reply Last reply
          0
          • C Cristi Posea

            Usually, you will want to draw in a window's device context. As Paul said above, you will get such a DC in handlers like OnPaint() or in OnDraw() for views. If you have a window, you can get the window's DC using GetDC(), draw into it, then use ReleaseDC(). As a rule of thumb, at least for the beginning, try to keep the painting stuf in OnDraw(). You can call from there helper functions, giving them a pointer to the provided DC (pDC is fine). Also, you can create a "memory DC", which basically behaves like a normal DC, but you will not see the results. However, you can copy (blit) the contents of such a DC into a normal window DC. Also, you can use a memory DC to draw on a bitmap. For example: CDC dc; dc.CreateCompatibleDC(NULL); // creates a mem DC compatible with the display. ... // paint here, etc. dc.DeleteDC(); Other device context are related to printers, metafiles, etc.

            H Offline
            H Offline
            Homber Martinez
            wrote on last edited by
            #5

            thank you all for your replies ... all i wanted to do is have a memory DC on which i would put a bitmap and manipulate it around and then put it on the device context i get in the OnDraw() ... the problem i ran into was that the memory context is 1x1 (as it should be i guess) but how can i do LineTo or FloodFill if it is of that size

            C 1 Reply Last reply
            0
            • H Homber Martinez

              isn't it possible to create my own device context to play around with ... in any member function void MyClass::myfunc(int i,int a) { CDC dc; dc.LineTo(10,10); }

              U Offline
              U Offline
              User 4744
              wrote on last edited by
              #6

              Add the GetDC() call from a CWnd derived class or you will have to pass it a windows handle. void MyClass::myfunc(int i,int a) { CDC * pDC = GetDC(); if ( pDC ) pDC->LineTo(10,10); }

              1 Reply Last reply
              0
              • H Homber Martinez

                thank you all for your replies ... all i wanted to do is have a memory DC on which i would put a bitmap and manipulate it around and then put it on the device context i get in the OnDraw() ... the problem i ran into was that the memory context is 1x1 (as it should be i guess) but how can i do LineTo or FloodFill if it is of that size

                C Offline
                C Offline
                Cristi Posea
                wrote on last edited by
                #7

                You have to create a bitmap and select it in the memory DC first. Once you selected your bitmap, all drawing changes it directly. See also the GDI section for examples of usage.

                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