Solved

The correct way to DeletObject

Posted on 2004-04-27
6
296 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now