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. what is the use of "pOldPen" here?

what is the use of "pOldPen" here?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicslearning
9 Posts 6 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.
  • B Offline
    B Offline
    bloodwinner
    wrote on last edited by
    #1

    the following code is from the examples on the book [code] CPen pen (PS_SOLID, 0, RGB (192, 192, 192)); CPen* pOldPen = dc.SelectObject (&pen); //do some drawing staff here dc.SelectObject (pOldPen); I know once I create a pen, I can do some drawing staff, but why should I add "CPen* pOldPen = dc.SelectObject (&pen)" and "dc.SelectObject (pOldPen)" here? what's the use of that?

    D 1 Reply Last reply
    0
    • B bloodwinner

      the following code is from the examples on the book [code] CPen pen (PS_SOLID, 0, RGB (192, 192, 192)); CPen* pOldPen = dc.SelectObject (&pen); //do some drawing staff here dc.SelectObject (pOldPen); I know once I create a pen, I can do some drawing staff, but why should I add "CPen* pOldPen = dc.SelectObject (&pen)" and "dc.SelectObject (pOldPen)" here? what's the use of that?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      It's called restoring the DC to its original state.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      B 1 Reply Last reply
      0
      • D David Crow

        It's called restoring the DC to its original state.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        B Offline
        B Offline
        bloodwinner
        wrote on last edited by
        #3

        sorry, what is that mean? could you explain it a little detailed? what is the difference if I had not use it?

        L 1 Reply Last reply
        0
        • B bloodwinner

          sorry, what is that mean? could you explain it a little detailed? what is the difference if I had not use it?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          bloodwinner wrote:

          what is the difference if I had not use it?

          You would get a "resource leak" (a GDI handle would be lost, and if your app runs for a long time, this leak would accumulate, which isn't good - leaks like this can easily crash Win9x for example). Basically, if a GDI object such as a pen, brush or font is still selected into a DC when it is destroyed, you will get a leak:

          void Draw(CDC& dc)
          {
          // Create a pen
          CPen pen;
          pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
          // Select the pen into a DC
          dc.SelectObject(&pen);
          // Draw
          ...
          // LEAK! Pen still selected into DC - remember that when a CPen goes out of scope,
          // the underlying pen HANDLE is automatically deleted.
          }

          To avoid this, do as your sample code shows (save the previous object when calling SelectObject), or save/restore the DC state like this:

          void Draw(CDC& dc)
          {
          // Save the current DC state
          int nState = dc.SaveDC();
          // Create a pen
          CPen pen;
          pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
          // Select the pen into a DC
          dc.SelectObject(&pen);
          // Draw
          ...
          // Restore original DC state (pen, font, brush, etc.).
          dc.RestoreDC(nState);
          }

          Last modified: 11hrs 24mins after originally posted --


          Kicking squealing Gucci little piggy.

          J 1 Reply Last reply
          0
          • L Lost User

            bloodwinner wrote:

            what is the difference if I had not use it?

            You would get a "resource leak" (a GDI handle would be lost, and if your app runs for a long time, this leak would accumulate, which isn't good - leaks like this can easily crash Win9x for example). Basically, if a GDI object such as a pen, brush or font is still selected into a DC when it is destroyed, you will get a leak:

            void Draw(CDC& dc)
            {
            // Create a pen
            CPen pen;
            pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
            // Select the pen into a DC
            dc.SelectObject(&pen);
            // Draw
            ...
            // LEAK! Pen still selected into DC - remember that when a CPen goes out of scope,
            // the underlying pen HANDLE is automatically deleted.
            }

            To avoid this, do as your sample code shows (save the previous object when calling SelectObject), or save/restore the DC state like this:

            void Draw(CDC& dc)
            {
            // Save the current DC state
            int nState = dc.SaveDC();
            // Create a pen
            CPen pen;
            pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
            // Select the pen into a DC
            dc.SelectObject(&pen);
            // Draw
            ...
            // Restore original DC state (pen, font, brush, etc.).
            dc.RestoreDC(nState);
            }

            Last modified: 11hrs 24mins after originally posted --


            Kicking squealing Gucci little piggy.

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #5

            That type of restore is more costly if I recall correctly. SaveState() saves everything. So, if you're only using a brush for instance, SaveState() will give you lots of overhead. On the other hand, CPU cycles aren't really a scarce resource these days.. :)

            -- A Stern Warning of Things to Come

            PJ ArendsP 1 Reply Last reply
            0
            • J Jorgen Sigvardsson

              That type of restore is more costly if I recall correctly. SaveState() saves everything. So, if you're only using a brush for instance, SaveState() will give you lots of overhead. On the other hand, CPU cycles aren't really a scarce resource these days.. :)

              -- A Stern Warning of Things to Come

              PJ ArendsP Offline
              PJ ArendsP Offline
              PJ Arends
              wrote on last edited by
              #6

              SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?

              Within you lies the power for good; Use it!

              J L H 3 Replies Last reply
              0
              • PJ ArendsP PJ Arends

                SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #7

                :laugh: Nah, Rob and I are just senile, and meant SaveDC/RestoreDC. :-D (Unless Rob really do know about some undocumented funcions :~)

                -- A Stern Warning of Things to Come

                1 Reply Last reply
                0
                • PJ ArendsP PJ Arends

                  SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Ooops. Yes, I meant SaveDC/RestoreDC - it was late when I made the post! :)


                  Kicking squealing Gucci little piggy.

                  1 Reply Last reply
                  0
                  • PJ ArendsP PJ Arends

                    SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?

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

                    Me too I wrote a program with SaveDC and RestoreDC:)

                    _**


                    **_

                    WhiteSky


                    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