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 save DC of onpaint(); in SDI MFC C++?

HOW to save DC of onpaint(); in SDI MFC C++?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
9 Posts 3 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.
  • H Offline
    H Offline
    humais
    wrote on last edited by
    #1

    how i can save dc of onpaint in MFC SDI c++? when i draw a rectangle with mouse,i can draw only on rectangle at a time??: confused:

    CPalliniC 1 Reply Last reply
    0
    • H humais

      how i can save dc of onpaint in MFC SDI c++? when i draw a rectangle with mouse,i can draw only on rectangle at a time??: confused:

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

      You shouldn't save the dc. If you need to paint multiple rectangles then do draw multiple rectangles in a single OnPaint body. :)

      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?

      H 1 Reply Last reply
      0
      • CPalliniC CPallini

        You shouldn't save the dc. If you need to paint multiple rectangles then do draw multiple rectangles in a single OnPaint body. :)

        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]

        H Offline
        H Offline
        humais
        wrote on last edited by
        #3

        okay but which fuctions i have to use to save dc & load dc again??

        H 1 Reply Last reply
        0
        • H humais

          okay but which fuctions i have to use to save dc & load dc again??

          H Offline
          H Offline
          humais
          wrote on last edited by
          #4

          i'm drawing rectangle with a mouse.but 1 rectangle is replaced by another...

          CPalliniC 1 Reply Last reply
          0
          • H humais

            i'm drawing rectangle with a mouse.but 1 rectangle is replaced by another...

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

            You must NOT save (or load) the device context. On the other hand, you must save the state of your drawing. For instance, once a rectangle has been draw (on mouse action) you should save it somewhere in you class. Whenever the OnPaint call occurs you have to redraw all the saved rectangles. :)

            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?

            H 1 Reply Last reply
            0
            • CPalliniC CPallini

              You must NOT save (or load) the device context. On the other hand, you must save the state of your drawing. For instance, once a rectangle has been draw (on mouse action) you should save it somewhere in you class. Whenever the OnPaint call occurs you have to redraw all the saved rectangles. :)

              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]

              H Offline
              H Offline
              humais
              wrote on last edited by
              #6

              okay but which functions i have to use to save the state of rectangles....plzz tell me the complete way..

              C CPalliniC 2 Replies Last reply
              0
              • H humais

                okay but which functions i have to use to save the state of rectangles....plzz tell me the complete way..

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                There's no function to do such a thing. You have to store all the rectangle information in a container (like for instance a std::list). If you don't know what a std::vector is, it is a kind of array of objects but which can grow dynamically. It is much easire to work with than C arrays. So, the principle is: once you moved the mouse to show an additional rectangle, instead of drawing it directly, you create an instance of your rectangle class and you add it to the vector, then you call Invalidate() in your view, which will force a redraw of the view. In the OnPaint method, you simply iterate over all your elements in your vector and draw them one by one.

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                1 Reply Last reply
                0
                • H humais

                  okay but which functions i have to use to save the state of rectangles....plzz tell me the complete way..

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

                  Cédric gave you the solution: after mouse action, store the (new) rectangle coordinates into an 'array of rectangle coordinates' (as suggested you may better use a STL container as the std::vector) and call in sequence InvalidateRect, UpdateWindow (to make re-paint happen), Inside the OnPaint message handler draw all the rectangles of the array. :)

                  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?

                  H 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Cédric gave you the solution: after mouse action, store the (new) rectangle coordinates into an 'array of rectangle coordinates' (as suggested you may better use a STL container as the std::vector) and call in sequence InvalidateRect, UpdateWindow (to make re-paint happen), Inside the OnPaint message handler draw all the rectangles of the array. :)

                    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]

                    H Offline
                    H Offline
                    humais
                    wrote on last edited by
                    #9

                    Thankx Cédric & CPallini, i hope this will help me :)

                    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