Freeing Selected BM from DC in GDI
-
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.
-
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.
-
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
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.
-
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.
-
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.
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: theCDC
class wraps a device-context and internally holds anHDC
, theCBitmap
class wraps a bitmap and internally holds anHBITMAP
and so on. When you callCDC::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 theFromHandle()
method (e.g. in case of a bitmap objectCBitmap::FromHandle()
is called). The pointers obtained in this way could be temporary because, if your application reach the idle state, i.e. itsCWinApp::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. -
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: theCDC
class wraps a device-context and internally holds anHDC
, theCBitmap
class wraps a bitmap and internally holds anHBITMAP
and so on. When you callCDC::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 theFromHandle()
method (e.g. in case of a bitmap objectCBitmap::FromHandle()
is called). The pointers obtained in this way could be temporary because, if your application reach the idle state, i.e. itsCWinApp::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.That is a great answer; I now understand completely. Thank You Tadeusz Westawic Sum quid sum.