Unclear on HDC and SelectObject
Hi people,
Im currently working on a project that requires me to change the font. Im using
Code:
SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT));
to do this and it works great.
The msdn states the following:
"This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object."
I do not understand why it is neccary to replace the object with the original, whats not helping is that some examples i look at do replace the original back and others dont.
Could someone help me with this?
Thanks very much!!
Re: Unclear on HDC and SelectObject
Handles, and especially DC's in general, are not an unlimited resource. If you use them up, you run the risk of your application failing.
Re: Unclear on HDC and SelectObject
There are good examples and bad examples. ;)
Well, it's a long story. I'll try to explain by short without include all cases.
If we got a common DC we have to release it when no more needed by calling ReleaseDC (as Mike already stated).
For a similar reason we have to delete created GDI objects (pen, brushes, bitmaps, etc.) when no more needed (see DeleteObject).
We must not delete objects currently selected in a DC.
Also it's a very bad ideea to delete objects currently selected in a DC, after ReleaseDC call.
So... call SelectObject to "set free" the GDI objects you have created, before release the DC and delete them.
In your particular case you have selected a stock object (font) so deleting it is not necessary (anyhow calling DeleteObject has no effect) so you can let it inside.
However, take this as a good practice: always "free" your objects from DC.
Another easier way to do that is using SaveDC and RestoreDC:
Code:
int nSavedDC = SaveDC(hDC);
// ...
HBRUSH hBrushBlue = CreateSolidBrush(RGB(0,0,255));
SelectObject(hDC, hBrushBlue);
// ...
RestoreDC(hDC, nSavedDC);
DeleteObject(hBrushBlue);
// ...
NOTE: The device context has stock objects initially selected as for example BLACK_PEN.
[ Redirected thread ]
Re: Unclear on HDC and SelectObject
I'd like to add one illustration to ovidiucucu's great explanation:
if you pass the DC to another function (like to an object, to draw itself), the callee expects that DC to be returned in its original states. Othervise, it will continue to use your stock font (might not be desired).
And one performance note: if you need to re-select more than two objects, go with SaveDC() / RestoreDC().
Re: Unclear on HDC and SelectObject
Wow, thats very much for the replys and thanku Odiucucu =) They have helped heaps.
I may come accross a couple more similar questions, if so ill keep this updated.
Cheeers to all!
Re: Unclear on HDC and SelectObject
Ok, thanks for all the help so far, i have just one more question (for now)..
In all the examples i have seen the programe saves the dc, does the printing and then restores it and deletes brushes etc.. the programe does all this EVERY time is prints ...
Is there any reason why they do it like this (it seems in-efficient) and why i could not not save the dc in a constructor and restore in the destructure while having a HDC member in the class? This way i only have to do it once instead of every time i paint..?
Thanks again
Re: Unclear on HDC and SelectObject
Quote:
Originally Posted by UnfitElf
Is there any reason why they do it like this (it seems in-efficient) and why i could not not save the dc in a constructor and restore in the destructure while having a HDC member in the class? This way i only have to do it once instead of every time i paint..?
Thanks again
The main reason can be seen in three words: to "avoid being selfish".
Anyhow the way you have suggested do not increase the efficiency at all (saving and restoring DC each time you paint take "infinitely" less time than painting itself).