Re: CFont frustration
From: Johan Rosengren (johan.rosengren_at_telia.com)
Date: 02/28/04
- Next message: Johan Rosengren: "Re: Setting the "Class Cursor""
- Previous message: Scott: "Re: Accessing procedures in another class"
- In reply to: Erik J Sawyer: "CFont frustration"
- Next in thread: Jase: "Re: CFont frustration"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 28 Feb 2004 20:53:03 +0100
Erik,
Your problem should not be with the CFont*, I would ratehr guess that you
are destroying your bold font too early.
First of all, if you want the font from dialogs, you could get it as a stock
object. You don't need any control for this, and might thus create the bold
font in the ctor. Then, you will want to destroy the same font in the dtor.
You can just forget about the font already existing for the control - you
don't need to reset it. Thus, we get:
CMyStatic::CMyStatic()
{
LOGFONT lf;
// Get the logfont for the default dialog font
m_fntOriginal.CreateStockObject(DEFAULT_GUI_FONT);
m_fntOriginal.GetLogFont(&lf);
// Get rid of the stock object. We don't need to delete it
m_fntOriginal.Detach();
// Modify the logfont and create our CFont
lf.lfWeight=FW_BOLD;
m_fntOriginal.CreateFontIndirect(&lf);
}
CMyStatic::~CMyStatic()
{
// Get rid of it, now, it's
// definitely not selected anywhere
m_fntOriginal.DeleteObject();
}
void CMyStatic::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
// Set our font
SetFont(&m_fntOriginal,FALSE);
}
Voilà. Now, the font is most certainly not selected when we delete it, and
the stock object we based it on needs not be deleted at all.
Johan Rosengren
Abstrakt Mekanik AB
"Erik J Sawyer" <anonymous@discussions.microsoft.com> a écrit dans le
message de news:8C5B9D9A-DBF3-450B-9556-EEFC7A695800@microsoft.com...
> I'm trying to create a static text field on a dialog, with a bold font. I
want to use the system/control assigned font, to allow flexibility at
runtime. Therefore, what I am trying to do is A) retrieve the pre-selected
font, B) use the LOGFONT to create an equivilent font that's bold, and C)
pass the new font to the control.
>
> All this works, but VC6 keeps telling me it's finding memory leaks and/or
access faults. These appear to be related to the (supposedly)
garbage-collected CFont* returned by GetFont(). Where am I going wrong?
>
> /* <codesnippet> */
> void CMyStatic::PreSubclassWindow()
> {
> // Get the current window font.
> CFont* pFont = GetFont();
>
> ASSERT_VALID(pFont);
>
> if (pFont) {
> m_fntOriginal.Attach(pFont->m_hObject);
>
> LOGFONT lf;
> pFont->GetLogFont(&lf);
> lf.lfWeight = FW_BOLD;
>
> if (m_fntBold.CreateFontIndirect(&lf))
> {
> CStatic::SetFont(&m_fntBold);
> }
> // tried the following, didn't make any difference
> // pFont->Detach();
> // pFont->m_hObject = NULL;
> }
>
> CStatic::PreSubclassWindow();
> }
>
> void CMyStatic::OnDestroy()
> {
> CStatic::SetFont(&m_fntOriginal);
> m_fntBold.DeleteObject();
> m_fntBold.m_hObject = NULL;
> CStatic::OnDestroy();
> }
>
> /* </codesnippet> */
>
> Thanks,
> Erik J Sawyer
> CFT Programmer
> Appro Systems
- Next message: Johan Rosengren: "Re: Setting the "Class Cursor""
- Previous message: Scott: "Re: Accessing procedures in another class"
- In reply to: Erik J Sawyer: "CFont frustration"
- Next in thread: Jase: "Re: CFont frustration"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|