?
Solved

The correct way to DeletObject

Posted on 2004-04-27
6
Medium Priority
?
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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