Solved

The correct way to DeletObject

Posted on 2004-04-27
6
301 Views
Last Modified: 2008-02-01
Hi,

I've read a ton of different info. on how to clean up bitmap objects but alot of the information is different.   Can someone explain what the correct method is for cleaning up a GDI object such as a bitmap. I.E.

HDC pDC = ::GetWindowDC(hWnd);
hBitmapDC = ::CreateCompatibleDC(pDC);
HBITMAP       hBitmap = ::LoadBitmap(hInst,(LPCTSTR)nResID);

::SelectObject(hBitmapDC,hBitmap );
::BitBlt(pDC,0,0,10,10,hBitmapDC,0,0,SRCCOPY);
                        
...How do I clean up properly?
0
Comment
Question by:alsmorris
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:MattAA
ID: 10930410
You clean up any bitmap loaded with LoadBitmap by calling DeleteObject on its handle.  If this was a pen or a brush, you would have to unselect it from the device context before you could delete it, but there shouldn't be any problems with bitmaps.  So, to cleanup after that code, you would simply put:

::DeleteObject(hBitmap);

You should also delete your memory device context (hBitmapDC) with ::DeleteDC when you are done with it.
0
 

Author Comment

by:alsmorris
ID: 10930494
MattAA,

I have tried this but I get resource errors... and finally the window goes black.  I am finding out that what works on 2000/XP does not work on 98.  For example what you said ::DeleteObject(hBitmap); will not give me a problem on 2000/xp but on 98 I get a black window and boundchecker tells me that the object is still selected in the DC.

Thanks :)
0
 
LVL 8

Accepted Solution

by:
_corey_ earned 100 total points
ID: 10931506
You need to repeat your SelectObject call first to make sure.

Select all objects out of the DC, then destroy the DC and the objects.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:alsmorris
ID: 10931570
ok ... I think I figured it out...But I'm not sure this is the best way to do it...

HDC pDC = ::GetWindowDC(hWnd);
hBitmapDC = ::CreateCompatibleDC(pDC);
HBITMAP      hBitmap = ::LoadBitmap(hInst,(LPCTSTR)nResID);

HBITMAP hOld = ::SelectObject(hBitmapDC,hBitmap );

::BitBlt(pDC,0,0,10,10,hBitmapDC,0,0,SRCCOPY);

::SelectObject(hBitmapDC,hOld);
::DeleteObject(hBitmap);
::DeleteDC(hBitmapDC);




0
 
LVL 8

Expert Comment

by:_corey_
ID: 10931600
That's about how DC/Object stuff goes.  You really need to keep it nicely cleared up for 98/etc because, if I'm not mistaken after this long, there are limited DC and GDI handles.
0
 
LVL 8

Expert Comment

by:_corey_
ID: 10931605
Actually, just repeating the SelectObject(hBitmapDC, hBitmap) call should restore by itself.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question