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. Graphics
  4. Freeing Selected BM from DC in GDI

Freeing Selected BM from DC in GDI

Scheduled Pinned Locked Moved Graphics
graphicsquestionannouncementlearning
6 Posts 3 Posters 6 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.
  • T Offline
    T Offline
    Tadeusz Westawic
    wrote on last edited by
    #1

    I have recently discovered that dc.SelectObject(&mybitmap); ... dc.SelectObject((CBitmap*)NULL); does not cause the release of mybitmap. What is the proper way to cause dc to release bitmap without assigning another "real" resource (return the dc to its freshly created state) so that another dc in another subroutine can select mybitmap? Tadeusz Westawic Sum quid sum.

    L 1 Reply Last reply
    0
    • T Tadeusz Westawic

      I have recently discovered that dc.SelectObject(&mybitmap); ... dc.SelectObject((CBitmap*)NULL); does not cause the release of mybitmap. What is the proper way to cause dc to release bitmap without assigning another "real" resource (return the dc to its freshly created state) so that another dc in another subroutine can select mybitmap? Tadeusz Westawic Sum quid sum.

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

      The first call to SelectObject will have returned a handle to the default bitmap in the DC. Store it and select it back in when you're finished.

      L u n a t i c F r i n g e

      T 1 Reply Last reply
      0
      • L Lost User

        The first call to SelectObject will have returned a handle to the default bitmap in the DC. Store it and select it back in when you're finished.

        L u n a t i c F r i n g e

        T Offline
        T Offline
        Tadeusz Westawic
        wrote on last edited by
        #3

        Yes, thank you, it is the way I used to do it. Let me ask this, then: what does the dc.SelectObject() doc (msdn) mean in explanation of return value, where it says about func may return ptr to temp object? That is the thing that scared me off the tried and true. Tadeusz Westawic Sum quid sum.

        L S 2 Replies Last reply
        0
        • T Tadeusz Westawic

          Yes, thank you, it is the way I used to do it. Let me ask this, then: what does the dc.SelectObject() doc (msdn) mean in explanation of return value, where it says about func may return ptr to temp object? That is the thing that scared me off the tried and true. Tadeusz Westawic Sum quid sum.

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

          See here[^], if you haven't already.

          L u n a t i c F r i n g e

          1 Reply Last reply
          0
          • T Tadeusz Westawic

            Yes, thank you, it is the way I used to do it. Let me ask this, then: what does the dc.SelectObject() doc (msdn) mean in explanation of return value, where it says about func may return ptr to temp object? That is the thing that scared me off the tried and true. Tadeusz Westawic Sum quid sum.

            S Offline
            S Offline
            Sauro Viti
            wrote on last edited by
            #5

            GDI works with handles: HDC for device-contexts, HBITMAP for bitmaps, and so on. MFC provides a wrapper class for each entity provided by GDI: the CDC class wraps a device-context and internally holds an HDC, the CBitmap class wraps a bitmap and internally holds an HBITMAP and so on. When you call CDC::SelectObject(), it returns a pointer to the appropriate MFC class that represents the GDI object previously selected into the device-context; these pointers are obtained calling the FromHandle() method (e.g. in case of a bitmap object CBitmap::FromHandle() is called). The pointers obtained in this way could be temporary because, if your application reach the idle state, i.e. its CWinApp::OnIdle() is called, the objects pointed are deleted; this doesn't mean that the underlaying handle is destroyed, but only that the wrapper class is destroyed. As a conseguence, you cannot store such a pointer to use it later: you should select it back into the device-context before returning from the current message handler.

            T 1 Reply Last reply
            0
            • S Sauro Viti

              GDI works with handles: HDC for device-contexts, HBITMAP for bitmaps, and so on. MFC provides a wrapper class for each entity provided by GDI: the CDC class wraps a device-context and internally holds an HDC, the CBitmap class wraps a bitmap and internally holds an HBITMAP and so on. When you call CDC::SelectObject(), it returns a pointer to the appropriate MFC class that represents the GDI object previously selected into the device-context; these pointers are obtained calling the FromHandle() method (e.g. in case of a bitmap object CBitmap::FromHandle() is called). The pointers obtained in this way could be temporary because, if your application reach the idle state, i.e. its CWinApp::OnIdle() is called, the objects pointed are deleted; this doesn't mean that the underlaying handle is destroyed, but only that the wrapper class is destroyed. As a conseguence, you cannot store such a pointer to use it later: you should select it back into the device-context before returning from the current message handler.

              T Offline
              T Offline
              Tadeusz Westawic
              wrote on last edited by
              #6

              That is a great answer; I now understand completely. Thank You Tadeusz Westawic Sum quid sum.

              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