Tech Support > Microsoft Windows > Development Resources > object handling question
object handling question
Posted by JG on September 25th, 2007


is this the correct way to reselect an object that was the default font
upon program startup? Is this a big deal or am I reading too much
into the doc's on object handling?
---------my code below---------
<....
other declares
.....>
HFONT font;
HGDIOBJ OriginalObject;

<....
font creation
.....>

OriginalObject = SelectObject(hDC, font); //save handle to old font, put in new
TextOut (hDC, 50, 50, InstallHeaderStr, InstallHeaderStrLen); //use new font

SelectObject (hDC, OriginalObject); // return original font ?

//Cleanup
SetTextColor ( hDC, BakupTextColor); //return old text color
DeleteObject (font); // delete created font

----Visual C++ documentation below----
HGDIOBJ SelectObject(
HDC hdc, // handle to device context
HGDIOBJ hgdiobj // handle to object
);

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.
--------
BOOL DeleteObject(
HGDIOBJ hObject // handle to graphic object
);

Do not delete a drawing object (pen or brush) while it is still selected
into a device context.



Posted by Ulrich Eckhardt on September 25th, 2007


JG wrote:
This is the correct way to do it, restore the DC's font to the one it came
with.

Yes, if you created the object you are responsible for deleting it. However:

| MSDN:
| Do not delete a drawing object (pen or brush) while it is still selected
| into a device context.

I think this also applies to fonts, at least it doesn't hurt to always
select the old object first. BTW: in C++, this is easiest achieved
using 'RAII', search the web for it.

Uli

--
Sator Laser GmbH
Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932


Posted by Norman Bullen on September 25th, 2007


JG wrote:
default font that can be changed once for the life time of the application?

Every time you get a device context from GetDC() or BeginPaint() it will
have the _system_ default font selected into it. You need to select your
favorite font into the DC every time and, when you're finished with the
DC, select the original font back into it.

Likewise, you need to select your text color every time you get a DC but
you don't need to select the original color back into it.

An alternative is to create windows with permanently associated DCs. See
window class styles CS_CLASSDC and CS_OWNDC.

Norm

--
Norm

To reply, change domain to an adult feline.


Posted by JG on September 25th, 2007


No actually I suspected this to be the case, just wanted to make sure I had
the other clean up stuff understood correctly.




Similar Posts